Learn to configure simple Java POJO objects as spring beans using java annotation based configuration. In this exmple, we will see the usage of @Configuration
, @ComponentScan
, @Bean
and @Scope
annotations.
Configure beans using @Bean annotation
- Use this approach when you do not want to annotate java POJO classes and want to centralize the bean definitions.
@Bean
annotation is used at method level. It indicates that a method produces a bean to be managed by the Spring container.- This annotation is an alternative to
<bean/>
element in the Spring XML configuration. - By default, name of the method is used as bean name. You can override the name using
name
attribute of the annotation. You can pass more than one name for a single bean, as well. - Use
@Scope
annotation to define the scope of the bean. - Use
@DependsOn
,@Primary
and@Lazy
to futher control the behavior of the bean.
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import com.springexamples.core.beans.Department; import com.springexamples.core.beans.Employee; @Configuration public class BeanConfig { @Bean @Scope(scopeName = "prototype") public Employee employee() { return new Employee(); } @Bean @Scope(scopeName = "prototype") public Department department() { return new Department(); } }
Demo
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class); System.out.println( ctx.getBean( Employee.class ) ); System.out.println( ctx.getBean( Department.class ) ); System.out.println( ctx.getBean( "employee" ) ); System.out.println( ctx.getBean( "department" ) ); } Output: [email protected] [email protected] [email protected] [email protected]
Configure beans using stereotype annotations
- Use this approach when you want to configure beans as POJO level.
- Spring has these stereotype annotation –
@Component
,@Service
,@Repository
and@Controller
. @Component
is a generic stereotype for any Spring-managed component.@Repository
,@Service
and@Controller
are specializations of@Component
.- Use
@Repository
to mark beans used at DAO/persistence layer. - Use
@Service
to mark beans used at service/manager layer. - Use
@Controller
to mark beans used at view controller layer. - Use
@Component
at any class where you are not able to decide, including above 3 layers. - Use
@ComponentScan
annotation at configuration class. It’sbasePackages
attribute is a common parent package where all bean definitions can be located. You can pass multiple paths in comma/semicolon/space-separated list.
@Component @Scope(scopeName = "prototype") public class Department { private Long id; private String name; //setters and getters } @Component @Scope(scopeName = "prototype") public class Employee { private Long id; private String firstName; private String lastName; private Department department; //setters and getters }
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan( basePackages = "com.springexamples.core" ) public class BeanConfig { }
Demo
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class); System.out.println( ctx.getBean( Employee.class ) ); System.out.println( ctx.getBean( Department.class ) ); System.out.println( ctx.getBean( "employee" ) ); System.out.println( ctx.getBean( "department" ) ); } Output: [email protected] [email protected] [email protected] [email protected]
Maven Dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springexamples.core</groupId> <artifactId>SpringExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringExample</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.1.RELEASE</version> </dependency> </dependencies> </project>