Spring Beans Auto-Wiring by R4R Team

Spring Beans Auto-Wiring:-
Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.
Usually we provide bean configuration details in the spring bean configuration file and we also specify the beans that will be injected in other beans using ref attribute. But Spring framework provides autowiring features too where we don’t need to provide bean injection details explicitly.

There are different ways through which we can autowire a spring bean.
1 no - This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring.

<bean id="employee" class="com.r4r.Employee">

<property name="name" ref="name" />

</bean>

<bean id="name" class="com.r4r.Name" />

2 byName – For this type of autowiring, setter method is used for dependency injection. Also the variable name should be same in the class where we will inject the dependency and in the spring bean configuration file.

<bean id="emloyeee" class="com.r4r.Emloyee" autowire="byName" />

<bean id="name" class="com.r4r.Name" />

3 byType – For this type of autowiring, class type is used. So there should be only one bean configured for this type in the spring bean configuration file.

<bean id="employee" class="com.r4r.Employee" autowire="byType" />

<bean id="name" class="com.r4r.Name" />

4 by constructor – This is almost similar to autowire byType, the only difference is that constructor is used to inject the dependency.

<bean id="employee" class="com.r4r.Employee" autowire="constructor" />

<bean id="name" class="com.r4r.Name" />

5 by autodetect – If you are on Spring 3.0 or older versions, this is one of the autowire options available. This option was used for autowire by constructor or byType, as determined by Spring container. Since we already have so many options, this option is deprecated.

<bean id="employee" class="com.r4r.Employee" autowire="autodetect" />

<bean id="name" class="com.r4r.Name" />

6 @Autowired annotation – We can use this annotation for spring bean autowiring. This annotation can be applied on variables and methods for autowiring byType. We can also use this annotation on constructor for constructor based autowiring.
For this annotation to work, we also need to enable annotation based configuraiton in spring bean configuration file. This can be done by context:annotation-config element or by defining a bean of type org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.

7 @Qualifier annotation – This annotation is used to avoid conflicts in bean mapping and we need to provide the bean name that will be used for autowiring. This way we can avoid issues where multiple beans are defined for same type. This annotation usually works with the @Autowired annotation. For constructors with multiple arguments, we can use this annotation with the argument names in the method.

Limitations with autowiring:-
Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing to developers to use it to wire only one or two bean definitions. Though, autowiring can significantly reduce the need to specify properties or constructor arguments but you should consider the limitations and disadvantages of autowiring before using them.
 

Limitations

Description

Overriding possibility

You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring.

Primitive data types

You cannot autowire so-called simple properties such as primitives, Strings, and Classes.

Confusing nature

Autowiring is less exact than explicit wiring, so if possible prefer using explict wiring.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!