There are many collaborating bean in Spring for develop an application, the autowire help in making the relationship between them. Bean Autowiring reduces the effort of writing properties or constructor arguments. Bean Autowiring is the feature provided by the spring framework to skip the some of configuration in XML file. The bean autowiring in specified at the autowire attribute inside <bean></bean> element.
<bean autowire="byName" class="com.dineshonjava.sdnext.beanautowiring.Triangle" id="triangle"></bean>
Popular Tutorials
There are five bean autowiring modes which can be used to instruct Spring container to use bean 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.. |
<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>
<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.
<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.
<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.
<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; }
Spring Related Topics you may like
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…