Configuring spring beans without XML

We have a class Person

  1. package com.kb.java_based_configuration;
  2.  
  3. public class Person {
  4.  
  5. }
package com.kb.java_based_configuration;

public class Person {

}

Now I want to make this as a spring bean.
I can do it using xml as follows
Method 1

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.    http://www.springframework.org/schema/context
  9.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <bean id = "person" class ="com.kb.java_based_configuration.Person">
  12. </beans>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id = "person" class ="com.kb.java_based_configuration.Person">
</beans>

Method 2
Annotate the class Person with stereotype annotation like @Component
And register it to scan in beans.xml file.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.    http://www.springframework.org/schema/context
  9.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10. <context:component-scan base-package="com.kb.java_based_configuration"/>
  11. </beans>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.kb.java_based_configuration"/>
</beans>

In both the methods we are using xml
Can we achieve the same without touching xml file ?
Yes we can do it by using just 2 annotation
a)@Configuration on class – same as beans tag in beans.xml
b)@Bean on method – same as bean tag in beans.xml

Let’s see with example

  1. package com.kb.java_based_configuration;
  2.  
  3. public class Person {
  4.  
  5. }
package com.kb.java_based_configuration;

public class Person {

}

Define a class which act as a spring beans tag and which should contain a method which produces bean.

  1. package com.kb.java_based_configuration;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class ApplicationBeans {
  8.    
  9.     @Bean(name="person")
  10.     public Person getPerson(){
  11.         return new Person();
  12.     }
  13.  
  14. }
package com.kb.java_based_configuration;

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

@Configuration
public class ApplicationBeans {
	
	@Bean(name="person")
	public Person getPerson(){
		return new Person();
	}

}

Now person bean is configured in the above configuration class.
The above class is the exact replacement of xml as below

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7.    http://www.springframework.org/schema/context
  8.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9. <bean id = "person" class ="com.kb.java_based_configuration.Person">
  10. </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id = "person" class ="com.kb.java_based_configuration.Person">
</beans>

How can we load or bootstrap the above bean ?
We can bootstrap the beans inside ApplicationBeans using AnnotationConfigApplicationContext

  1. package com.kb.java_based_configuration;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. public class Client {
  7.     public static void main(String[] args) {
  8.         ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationBeans.class);
  9.         Person p =  context.getBean("person",Person.class);
  10.         System.out.println(p);
  11.     }
  12.  
  13. }
package com.kb.java_based_configuration;

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

public class Client {
	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationBeans.class);
		Person p =  context.getBean("person",Person.class);
		System.out.println(p);
	}

}

No single xml line is written to achieve our so called spring bean definition inside container.

Note: we can write β€˜n’ no of methods inside single configuration class to define multiple beans but all methods should be annotated with @Bean.

Injecting values to bean using @Bean
Look at the @Bean annotated method how we inject the values to the bean.

  1. package com.kb.java_based_configuration;
  2.  
  3. public class Person {
  4. String name;
  5. int age;
  6. public String getName() {
  7.     return name;
  8. }
  9. public void setName(String name) {
  10.     this.name = name;
  11. }
  12. public int getAge() {
  13.     return age;
  14. }
  15. public void setAge(int age) {
  16.     this.age = age;
  17. }
  18. }
package com.kb.java_based_configuration;

public class Person {
String name;
int age;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
}

ApplicationBeans.java

  1. package com.kb.java_based_configuration;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6.  
  7. @Configuration
  8.  
  9. public class ApplicationBeans {
  10.    
  11.     @Bean(name="person")
  12.     public Person getPerson(){
  13.         Person p = new Person();
  14.         p.setName("kb");
  15.         p.setAge(26);
  16.         return p;
  17.     }
  18.  
  19. }
package com.kb.java_based_configuration;

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


@Configuration

public class ApplicationBeans {
	
	@Bean(name="person")
	public Person getPerson(){
		Person p = new Person();
		p.setName("kb");
		p.setAge(26);
		return p;
	}

}

Client.java

  1. package com.kb.java_based_configuration;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. public class Client {
  7.     public static void main(String[] args) {
  8.         ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationBeans.class);
  9.         Person p =  context.getBean("person",Person.class);
  10.         System.out.println(p.getName());
  11.         System.out.println(p.getAge());
  12.     }
  13.  
  14. }
package com.kb.java_based_configuration;

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

public class Client {
	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationBeans.class);
		Person p =  context.getBean("person",Person.class);
		System.out.println(p.getName());
		System.out.println(p.getAge());
	}

}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology πŸ™‚
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on