Configuring spring beans without XML

We have a class Person

1
2
3
4
5
package com.kb.java_based_configuration;
 
public class Person {
 
}
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
2
3
4
5
6
7
8
9
10
11
12
<?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>
<?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
2
3
4
5
6
7
8
9
10
11
<?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>
<?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
2
3
4
5
package com.kb.java_based_configuration;
 
public class Person {
 
}
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
2
3
4
5
6
7
8
9
10
11
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();
    }
 
}
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
2
3
4
5
6
7
8
9
10
<?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>
<?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
2
3
4
5
6
7
8
9
10
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);
    }
 
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
    }
 
}
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
2
3
4
5
6
7
8
9
10
11
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());
    }
 
}
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

Karibasappa G C (KB)
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