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
Bean Autowiring Modes
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.. |
1. Auto-Wiring “no”
<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”
<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”
<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”
<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”
<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; }
- confusing nature
- primitive data type
- overriding possibility
Spring Related Topics you may like
- Spring Interview Questions and Answers
- Spring AOP Interview Questions and Answers
- Spring MVC Interview Questions
- Spring Security Interview Questions and Answers
- Spring REST Interview Questions and Answers
- Spring Boot Interview Questions and Answers
- Spring Boot Microservices Interview Questions and Answers
- Dependency Injection (DI) in Spring
- Spring IoC Container
- What is Bean Factory in Spring
- ApplicationContext in Spring
- Bean Autowiring in Spring
- Spring Bean Scopes
- Create Custom Bean Scope in Spring Example
- Using ApplicationContextAware in Spring
- Spring Bean Life Cycle and Callbacks
- BeanPostProcessor in Spring
- BeanFactoryPostProcessor in Spring
- Annotations in Spring and Based Configuration
- Spring JSR-250 Annotations
- JSR 330 Annotations in Spring
- Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
- Method injection with Spring using Lookup method property
- Spring AOP-Introduction to Aspect Oriented Programming
- @Aspect Annotation in Spring
- Spring AOP AspectJ @Before Annotation Advice Example
- Spring AOP Before Advice Example using XML Config
- Spring AOP AspectJ @After Annotation Advice Example
- Spring AOP After Advice Example using XML Config
- Spring AOP AspectJ @AfterReturning Annotation Advice Example
- Spring AOP After-Returning Advice Example using XML Config
- Spring AOP AspectJ @AfterThrowing Annotation Advice Example
- Spring AOP After Throwing Advice Example using XML Config
- Spring AOP AspectJ @Around Annotation Advice Example
- Spring AOP Around Advice Example using XML Config
- Spring AOP Proxies in Spring
- Spring AOP Transaction Management in Hibernate
- Spring Transaction Management
- Spring Declarative Transaction Management Example
- Spring AOP-Ordering of Aspects with Example
- Spring Security Java Based Configuration with Example
- Spring Security XML Namespace Configuration Example
nice gud…
great work
Thanks for showing the configuration and not just listing the types of auto-wiring.