In this article we will see how to inject beans using spring @Autowired
annotation.
An @Autowired
annotation can be used at a constructor, field, setter method or config method.
Define the beans to be autowired
Let’s first define few beans. Each bean is annotated with @Component
.
BeanA:
package dependents; import org.springframework.stereotype.Component; @Component public class BeanA { public BeanA() { System.out.println("BeanA() called"); } }
BeanB:
package dependents; import org.springframework.stereotype.Component; @Component public class BeanB { public BeanB() { System.out.println("BeanB() called"); } }
BeanC:
package dependents; import org.springframework.stereotype.Component; @Component public class BeanC { public BeanC() { System.out.println("BeanC() called"); } }
Autowire the beans using @Autowired
We will now define a bean which depends on all the above defined beans. We will use @Autowired
annotation to autowire the beans at field, constructor and method level.
Config:
package config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; import dependents.BeanA; import dependents.BeanB; import dependents.BeanC; @Component("config") @ComponentScan(basePackageClasses = BeanA.class) public class Config { private BeanC beanC; @Autowired private BeanA beanA; private BeanB beanB; @Autowired public Config(BeanC beanC) { System.out.println("Config1(BeanC beanC) called"); this.beanC = beanC; } @Autowired public void setBeanB(BeanB beanB) { this.beanB = beanB; } public String toString() { return "Beans: " + beanA + beanB + beanC; } }
Let’s now load the context. We just have to load Config
. This in turn will load the dependent beans using the @ComponentScan
.
SpringAutowiredAnnotationExample:
package com.javarticles.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import config.Config; public class SpringAutowiredAnnotationExample { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); try { ctx.register(Config.class); ctx.refresh(); System.out.println(ctx.getBean("config")); } finally { ctx.close(); } } }
Output:
Feb 12, 2016 11:23:13 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.spring[email protected]7c3df479: startup date [Fri Feb 12 23:23:13 IST 2016]; root of context hierarchy BeanC() called Config1(BeanC beanC) called BeanA() called BeanB() called Beans: [email protected]@[email protected] Feb 12, 2016 11:23:13 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose INFO: Closing org.spring[email protected]7c3df479: startup date [Fri Feb 12 23:23:13 IST 2016]; root of context hierarchy
Download the source code
This was an example about spring @Autowired annotation.
You can download the source code here: springAutowiredAnnotationExample.zip