Configuring spring beans without XML
We have a class Person
- 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
- <?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.
- <?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
- 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.
- 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
- <?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
- 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.
- 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
- 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
- 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()); } }
What’s the difference between and
please explain
Thanks Shailendra !!
I have already done this for Spring MVC and Spring Security related posts where i have given an option to download the entire project.
But for core concepts , i have not done this, I will do it for sure, Please give sometime.
Thanks for alarming on this!!
Sir your website is very nice but some problem
You are not mension jar files ig we run your app how to know which jar files are support yor app
pls sir mention jar files i’m waiting……………….
Never understood the Spring concepts so clearly from any other tutorial.Great Work!!
Thank you so much Shilpa !! , Happy learning π
Difference between component-scan and annotation-config in Spring has to more to do with you want to auto wire the beans or also auto discover the beans. Using context:component-scan will not only auto wire the beans but also auto discover the beans.
http://netjs.blogspot.com/2016/04/difference-between-component-scan-and-annotation-config-spring.html
Yes, You are absolutely right , thats the gist of my below article
http://javainsimpleway.com/contextcomponent-scan-vs-contextannotation-config-2/
Thank you !!
HI, karibasappagc.
I agree with basavaraj.
very clear and informative tutorial. I enjoy reading it last 2 days π
Great Thanks!
Thank you Artem, Happy learning …
Cool…Thanks Man..happy learning..
i did not seen any tutorial like this, i really thank full for sharing your valuable knowledge in spring with good and valid examples.
i have plan to learn spring long back, i search many tutorials there i find only theory kind of stuffs, i find your java guide this is very useful and now i feel happy. i proudly say i learned spring.
thank you karibasappa sir