Learn spring boot at https://www.javaguides.net/p/spring-boot-tutorial.html
Video Tutorial
1. Spring Boot CommandLineRunner interface overview
2. How to use CommandLineRunner
- Using CommandLineRunner as @Component (spring component)
- Implement CommandLineRunner in Spring Main class (Annotated with @SpringBootApplication annotation)
- Using CommandLineRunner as Bean (using @Bean annotation)
1) Using CommandLineRunner as @Component
@Component
public class ApplicationRunner implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println(" ApplicationRunner called");
}
}
2) Implement CommandLineRunner in @SpringBootApplication
@SpringBootApplication
public class SpringBootWebApplication implements CommandLineRunner {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@Override
public void run(String...args) throws Exception {
System.out.println("Application Started !!");
}
}
3) Using CommandLineRunner as Bean
package net.javaguides.springboot;
import org.springframework.boot.CommandLineRunner;
public class ApplicationRunner implements CommandLineRunner {
@Override
public void run(String...args) throws Exception {
System.out.println(" ApplicationRunner called");
}
}
package net.javaguides.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootWebApplication {
@Bean
public ApplicationRunner applicationStartupRunner() {
return new ApplicationRunner();
}
public static void main(String[] args) {
SpringApplication.run(SpringBooWebApplication.class, args);
}
}
3. Using @Order if multiple CommandLineRunner interface implementations
@Order(value=3)
@Component
class BeanOne implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
System.out.println("BeanOne run method Started !!");
}
}
@Order(value=2)
@Component
class BeanTwo implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
System.out.println("BeanTwo run method Started !!");
}
}
@Order(value=1)
@Component
class BeanThree implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
System.out.println("BeanThree run method Started !!");
}
}
4. Insert Database Records while Application started
MyRunner Class
package net.guides.springboot.jparepository;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import net.guides.springboot.jparepository.model.Employee;
import net.guides.springboot.jparepository.repository.EmployeeRepository;
@Component
public class MyRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(MyRunner.class);
@Autowired
private EmployeeRepository employeeRepository;
@Override
public void run(String...args) throws Exception {
employeeRepository.save(new Employee("Ramesh", "Fadatare", "ramesh@gmail.com"));
employeeRepository.save(new Employee("Tom", "Cruise", "tom@gmail.com"));
employeeRepository.save(new Employee("John", "Cena", "john@gmail.com"));
employeeRepository.save(new Employee("tony", "stark", "stark@gmail.com"));
logger.info("# of employees: {}", employeeRepository.count());
logger.info("All employees unsorted:");
Iterable < Employee > employees = employeeRepository.findAll();
Iterator < Employee > iterator = employees.iterator();
while (iterator.hasNext()) {
logger.info("{}", iterator.next().toString());
}
logger.info("------------------------");
logger.info("Deleting employee with id 1");
employeeRepository.deleteById(1 L);
logger.info("# of employees: {}", employeeRepository.count());
employeeRepository.existsById(2 L);
employeeRepository.findById(2 L);
}
}
Read the complete example at https://www.javaguides.net/2019/06/spring-boot-crudrepository-example-tutorial.html.
5. Conclusion
Follow me at https://www.javaguides.net/p/about-me.html
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment