dineshonjava

Bean Autowiring in Spring

There are many collaborating bean in Spring for develop an application, the autowire help in making the relationship between them. Autowiring reduces the effort of writing properties or constructor argumentsAutowiring is the feature provided by the spring framework to skip the some of configuration in XML file.The autowiring in specified at the autowire attribute inside <bean></bean> element.
<bean autowire="byName" class="com.dineshonjava.sdnext.beanautowiring.Triangle" id="triangle"></bean>

Bean Autowiring in Spring

Autowiring Modes: There are five autowiring modes which can be used to instruct Spring container to use autowiring for dependency injection. In above syntax we have used the autowire attribute of the element to specify autowire mode for a bean definition.

 Mode Explanation
no It is default which define no autowiring.
byName Autowiring is done by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.
byType Autowiring is done by matching data type of property name. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown.
constructor Autowiring is done by matching data type of property name with the property constructor argument. i.e. Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
autodetect When default constructor with no argument, it auto-wire by data type or it auto-wire by constructor means that Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType..

1. Auto-Wiring "no"

This is the default mode, you need to wire your bean via ‘ref’ attribute.
<bean class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">
   <property name="center" ref="center">
</property></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>

2. Auto-Wiring "byName"

Auto-wire a bean by property name. In this case, since the name of "center" bean is same with the name of the "circle" bean’s property ("center"), so, Spring will auto wired it via setter method – "setCenter(Point center)".
<bean autowire="byName" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>
Circle.java
public class Circle{
    private Point center;
    --
    public void setCenter(Point center){
       this.center = center;
    }
Click for see the full Example with Triangle class.

3. Auto-Wiring "byType"

Auto-wire a bean by property data type. In this case the data type of "center" bean is same with the data type of the "circle" bean’s property ("center"), so, Spring will auto wired it via setter method – "setCenter(Point center)".
<bean autowire="byType" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>
Circle.java
public class Circle{
    private Point center;
    --
    public void setCenter(Point center){
       this.center = center;
    }
Click for see the full Example with Circle class.

4. Auto-Wiring "constructor"

Auto-wire a bean by property data type in constructor argument. In this case, since the data type of "center" bean is same as the constructor argument data type in "circle" bean’s property (Point center), so, Spring auto wired it via constructor method – "public Circle(Point center)".
<bean autowire="constructor" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>
Circle.java
public class Circle{
    private Point center;
    --
    public Center(Point center){
       this.center = center;
    }
Click for see the full Example with Circle class.

5. Auto-Wiring "autodetect"

If a default constructor is found, uses "constructor"; Otherwise, uses "byType". In this case, since there is a default constructor in "Circle" class, so, Spring auto wired it via constructor method – "public Circle(Point center)".
<bean autowire="autodetect" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>
Circle.java
public class Circle{
    private Point center;
    --
    public Center(Point center){
       this.center = center;
    }
Click for see the full Example with Circle class.
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. Following limitations are
  • confusing nature
  • primitive data type
  • overriding possibility
Spring Related Topics you may like
  1. Spring Interview Questions and Answers
  2. Spring AOP Interview Questions and Answers
  3. Spring MVC Interview Questions
  4. Spring Security Interview Questions and Answers
  5. Spring REST Interview Questions and Answers
  6. Spring Boot Interview Questions and Answers
  7. Spring Boot Microservices Interview Questions and Answers
  8. Dependency Injection (DI) in Spring
  9. Spring IoC Container
  10. What is Bean Factory in Spring
  11. ApplicationContext in Spring
  12. Bean Autowiring in Spring
  13. Spring Bean Scopes
  14. Create Custom Bean Scope in Spring Example
  15. Using ApplicationContextAware in Spring
  16. Spring Bean Life Cycle and Callbacks
  17. BeanPostProcessor in Spring
  18. BeanFactoryPostProcessor in Spring
  19. Annotations in Spring and Based Configuration
  20. Spring JSR-250 Annotations
  21. JSR 330 Annotations in Spring
  22. Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
  23. Method injection with Spring using Lookup method property
  24. Spring AOP-Introduction to Aspect Oriented Programming
  25. @Aspect Annotation in Spring
  26. Spring AOP AspectJ @Before Annotation Advice Example
  27. Spring AOP Before Advice Example using XML Config
  28. Spring AOP AspectJ @After Annotation Advice Example
  29. Spring AOP After Advice Example using XML Config
  30. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  31. Spring AOP After-Returning Advice Example using XML Config
  32. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  33. Spring AOP After Throwing Advice Example using XML Config
  34. Spring AOP AspectJ @Around Annotation Advice Example
  35. Spring AOP Around Advice Example using XML Config
  36. Spring AOP Proxies in Spring
  37. Spring AOP Transaction Management in Hibernate
  38. Spring Transaction Management
  39. Spring Declarative Transaction Management Example
  40. Spring AOP-Ordering of Aspects with Example
  41. Spring Security Java Based Configuration with Example
  42. Spring Security XML Namespace Configuration Example
In later chapter we will discuss autowiring with annotation. @Autowired annotation, is more flexible and recommended.


In Next Chapter we will discuss about Spring Beans Scope in Application.


                                                                      << Previous || Next >>