Spring 3 Bean Configuration

In this article, you will learn to write Spring beans and configure them using pure Java based configuration instead of using XML.

XML with bean definitions:

<bean id="employee" class="com.kruders.bean.Employee">
    <property name="name" value="Puneet" />
    <property name="address" ref="address" />
</bean>

AppContext configuration class that contains bean definitions:

@Configuration
public class Config {
	
	@Bean(name="employee")
    public Employee employee() {
		Employee employee = new Employee();
		employee.setName("Puneet");
        return employee;
    }
}

@Configuration indicates that a class declares one or more Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

Following example demonstrates Spring Configuration :

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>
</dependencies>

1. JavaConfig

The @Configuration annotation tells the Spring container that this is a configuration class having bean definitions and dependencies. The @Bean annotation is used to define the bean. The method name is the same as bean id or name by default. The return type of the method is the bean that is registered to Spring application context. You use setter methods of the bean to set the dependencies and the container will invoke them to wire the associations.

package com.kruders.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.kruders.bean.Employee;

@Configuration
public class Config {
	
	@Bean(name="employee")
    public Employee employee() {
		Employee employee = new Employee();
		employee.setName("Puneet");
        return employee;
    }
}

2. POJO

Now create Employee Class as following.

package com.kruders.bean;

public class Employee {
	private String name;
		
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

3. Run Program

Create Main.java class that displays the employee details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.kruders.bean.Employee;
import com.kruders.config.Config;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
	    
		Employee employee = (Employee) context.getBean("employee");
			
		System.out.println("Name " + employee.getName());
	}

}

When you run the above example you’ll get an output like:

Name Puneet

The folder structure of the example is shown below in Figure 32.1

Figure 32.1 Figure 32.1

You can download the source code of this example here.

,

No comments yet.

Leave a Reply