Spring Autowiring

Spring Autowiring

Spring autowiring is the process in which we define the relationship between objects with the help of XML based autowire attribute on <bean/> element or @Autowired annotation for Annotation based configuration.

Then Spring container resolves those relationships automatically by reading the contents of ApplicationContext. There are some advantages of using autowiring

  1. Normally we specify dependencies with the help of constructor arguments or property set. But with the help of auto wire, we don’t need to specify them they are automatically resolved.
  2. It reduces the amount of code we write to specify dependencies.
  3. Autowiring can update the configuration as new dependency evolves.

You can also instruct the spring IoC container on how your dependencies should be resolved by specifying autowiring mode as below

Mode Description
no (Default) no autowiring. Bean can only be resolved by using ref element
byName Autowire a bean with the name. Spring looks for the bean name in order to set the property.
byType Autowire a bean by its class type. But in this case, there should be only one bean exist in the container. Otherwise exception will be thrown.
constructor It is same as byType but applies to constructor arguments only. It looks for the same type if not found then a exception will be thrown.

There are some limitations with autowiring as well
  1. The property and constructor-arg will always override the autowiring settings. Because @annotation is processed before XML configuration.
  2. You can’t autowire primitive data types such as int, float etc.
  3. Wiring information may not be available to the tools that generates documentation from spring container.

The autowire-candidate

Spring provides you facility to make particular bean unavailable for autowiring infrastructure. You can do this by simple providing true or false value to autowire-candidate attribute of <bean/> elment.

You can also specify bean names or pattern-matching against bean names as value to default-autowire-candiates on root <beans/> element. For example, you can provide pattern like *Services which will look for the bean names ends with Services and make them available for autowiring.

The @Required Annotation

The @Required annotation simple indicates that particular bean property or bean should be resolved during initialization time.
Public class ServiceBean {

 Private UserDao userDao;

 Public void setUserDao(UserDao userDao) {
 This.userDao = userDao;

The @Autowired annotation

The @Autowired simply resolves dependencies automatically. @Autowired annotation can be applied to constructor with some arguments, method with single argument, method with multiple arguments.
public class ServiceBean {

 private UserDao userDao;
 private ProfileDao profileDao;
 private String errorMessage, successMessage;

 public ServiceBean(UserDao userDao) {
 this.userDao = userDao;

 public void setProfileDao(ProfileDao profileDao) {
 this.profileDao = profileDao;

 @Autowired(required = false)
 public void setMessages(String errorMessage, String successMessage) {
 this.errorMessage = errorMessage;
 this.successMessage = successMessage;

The @Primary annotation

When you specify autowiring byType and there are more than one bean definition which shares the same type. As per spring framework, there will be an exception when spring container will try to resolve the dependencies. To resolve this problem, we need to instruct the container to look for the primary bean and only use that bean to resolve the dependency.

XML Based
 <bean class="com.package.ServiceBean" primary="true">

 <bean class="com.package.ServiceBean">
Annotation Based
public class ServiceImpl {

 public ServiceBean userService() { }

 public ServiceBean listService() { }


Related Post

Leave a Reply

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