Example on loading spring components using Spring JavaConfig approach.

1. Bean Examples

For Administrator.java bean, please refer to Spring Java Config Example

Add User.java bean and define it as a component

package com.codesimplify;

import org.springframework.stereotype.Component;

@Component(value="userBean")
public class User {
	private String firstName;
	private String lastName;
	private String age;

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	public String toString(){
		return "UserBean: firstName - "+firstName +", lastName -"+lastName +", Age - "+age;
	}

}

User.java is defined as Spring component using @Componenet annotation and given a value of “userBean”. So User bean can be retrieved from Spring context using “userBean”

2. Configuration class with component scan example

package com.codesimplify;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages="com.codesimplify")
public class AppConfig {

	@Bean
	public Administrator adminBean(){
		Administrator admin=new Administrator();
		admin.setFirstName("john");
		admin.setLastName("smith");
		return admin;
	}
	
}

@ComponentScan(basePackages="com.codesimplify") – This annotation forces the configuration to scan for any components defined under “com.codesimplify” package.

Following example snippet is the Spring configuration through XML for component scan

<context:component-scan base-package="com.codesimplify">
</context:component-scan>

3. Example Application class that loads Spring Java Configuration

package com.codesimplify;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {

	public static void main(String[] args) {
		
		AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);
		// Getting User bean using name given
		User user1=(User)context.getBean("userBean");
		user1.setFirstName("Adam");
		user1.setLastName("Smith");
		user1.setAge("30");
		// Getting same user Bean using Class name. Here we don't need to cast.
		User user2=context.getBean(User.class);
		// Since Spring beans are Singletons by default, we will get user1 bean data to be printed below.
		System.out.println(user2);
	}
}

4. Output:

21:04:18.596 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userBean'
21:04:18.596 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userBean'
UserBean: firstName - Adam, lastName -Smith, Age - 30

5. References

Spring JavaConfig – Component Scan Example
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *