Learn to create spring beans using java configuration using annotations for any standalone application. We will learn to create it with and without scanning of component annotations and using @Bean
annotations.
Table of contents 1. Annotation configuration with component scanning 2. Using @Bean and @Configuration annotations
1. Annotation configuration with component scanning
Creating beans using component scanning can be done in two steps.
1.1. Annotate beans with respective component annotations
We shall use use one of following four annotations as appropriate.
@Component
@Repository
@Service
@Controller
Read More: Spring Component Annotations
package com.howtodoinjava.spring.service.impl; import org.springframework.stereotype.Service; import com.howtodoinjava.spring.model.Employee; import com.howtodoinjava.spring.service.EmployeeManager; @Service public class EmployeeManagerImpl implements EmployeeManager { @Override public Employee create() { Employee emp = new Employee(); emp.setId(1); emp.setName("Lokesh"); return emp; } }
1.2. Include bean packages in @ComponentScan
annotation
@Configuration @ComponentScan(basePackages = "com.howtodoinjava.spring.service") public class AppConfig { }
1.3. Demo
package com.howtodoinjava.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.howtodoinjava.spring.model.Employee; import com.howtodoinjava.spring.service.EmployeeManager; public class Main { public static void main( String[] args ) { //Method 1 //ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); //Method 2 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); EmployeeManager empManager = ctx.getBean(EmployeeManager.class); Employee emp = empManager.create(); System.out.println(emp); } }
Program Output:
Employee [id=1, name=Lokesh]
2. Using @Bean and @Configuration annotations
To create spring application content using @Bean
annotations, use these steps –
2.1. Create Java bean classes (no annotation or anything needed)
public class EmployeeManagerImpl implements EmployeeManager { @Override public Employee create() { Employee emp = new Employee(); emp.setId(1); emp.setName("Lokesh"); return emp; } }
2.2. Create @Bean annotation methods in configuration class
package com.howtodoinjava.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.howtodoinjava.spring.service.EmployeeManager; import com.howtodoinjava.spring.service.impl.EmployeeManagerImpl; @Configuration public class AppConfig { @Bean public EmployeeManager employeeManager() { return new EmployeeManagerImpl(); } }
2.3. Demo
package com.howtodoinjava.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.howtodoinjava.spring.model.Employee; import com.howtodoinjava.spring.service.EmployeeManager; public class Main { public static void main( String[] args ) { //Method 1 //ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); //Method 2 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); EmployeeManager empManager = ctx.getBean(EmployeeManager.class); Employee emp = empManager.create(); System.out.println(emp); } }
Program Output:
Employee [id=1, name=Lokesh]
That’s pretty much two simple and easy ways to create spring beans using pure java code and annotations.
Happy Learning !!
Well, it is very clear to explain the steps:
1. Annotation based configuration with component scanning
2. Using @Bean and @Configuration annotations
I believe most people will use the 1st one.