SpringExamples

Spring bean java configuration example

By Admin | Filed Under: Spring Core

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

  1. Use this approach when you do not want to annotate java POJO classes and want to centralize the bean definitions.
  2. @Bean annotation is used at method level. It indicates that a method produces a bean to be managed by the Spring container.
  3. This annotation is an alternative to <bean/> element in the Spring XML configuration.
  4. 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.
  5. Use @Scope annotation to define the scope of the bean.
  6. 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

  1. Use this approach when you want to configure beans as POJO level.
  2. Spring has these stereotype annotation – @Component, @Service, @Repository and @Controller.
  3. @Component is a generic stereotype for any Spring-managed component.
  4. @Repository, @Service and @Controller are specializations of @Component.
  5. Use @Repository to mark beans used at DAO/persistence layer.
  6. Use @Service to mark beans used at service/manager layer.
  7. Use @Controller to mark beans used at view controller layer.
  8. Use @Component at any class where you are not able to decide, including above 3 layers.
  9. Use @ComponentScan annotation at configuration class. It’s basePackages 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>
  • « Previous Page
  • 1
  • 2
  • 3

Spring Boot Tutorial

  • Hello world example
  • Which main class
  • Print all beans
  • Embedded jetty server
  • CommandLineRunner
  • Custom banner
  • Change Root Path and Server Port

Spring Tutorial

  • IoC Container
  • Java Configuration

Spring WebMVC Tutorial

  • Minimum required config

Copyright © 2016 · springexamples.com · All Rights Reserved. | Sitemap