English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The auto wiring feature of the Spring framework allows you to implicitly inject object dependencies. It uses setter or constructor injection internally.
Auto wiring cannot be used to inject primitive values and strings. It is only suitable for references.
It needs Less code, because we do not need to write code to explicitly inject dependencies.
Without the control of the programmer.
It cannot be used for primitive values and string values. It can only be used for references.
There are many auto wiring patterns:
Pattern | Description |
no | This is the default auto wiring pattern. This means that there is no auto wiring by default. |
byName | The byName pattern injects object dependencies based on the name of the bean. In this case, the property name and bean name must be the same. It internally calls the setter method. |
byType | The byType pattern injects object dependencies based on type. Therefore, the property name and bean name can be different. It internally calls the setter method. |
constructor | The constructor pattern injects dependencies by calling the constructor of the class. It calls the constructor with a large number of parameters. |
autodetect | from Spring 3is not recommended to use at the beginning. |
Let's see the simple code for auto wiring in Spring. You need to use the autowire attribute of the bean element to apply the auto wiring pattern.
<bean id="a" class="org.sssit.A" autowire="byName"></bean>
Let's take a look at a complete example of Spring auto wiring. To create this example, we created4files.
B.java A.java applicationContext.xml Test.java
B.java
This class only includes constructor and methods.
package org.sssit; public class B { B(){System.out.println("b is created");} void print(){System.out.println("hello b");} }
A.java
This class includes a reference to class B and constructor and methods.
package org.sssit; public class A { B b; A(){System.out.println("a is created");} public B getB() { return b; } public void setB(B b) { this.b = b; } void print(){System.out.println("hello a");} void display(){ print(); b.print(); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xmlns:p="http://www.springframework.org/schema/p xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="b" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="byName"></bean> </beans>
Test.java
This class gets Bean from applicationContext.xml file and calls the display method.
package org.sssit; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); A a=context.getBean("a",A.class); a.display(); } }
Output:
b is created a is created hello a hello b
Under the byName automatic assembly mode, the bean ID and reference name must be the same.
Use setter injection internally.
<bean id="b" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="byName"></bean>
However, if the bean name is changed, it will not inject dependencies.
Let's see how to change the bean name from b to b1code.
<bean id="b1" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="byName"></bean>
Under the byType automatic assembly mode, the bean ID and reference name may be different. However, there can only be one bean of the same type.
Internally uses setter injection.
<bean id="b1" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="byType"></bean>
In this case, it can work normally because you have created an instance of type B. It doesn't matter, you can use a bean name different from the reference name.
But if you have multiple beans of the same type, it will not work and throw an exception.
Let's see that there are many B-type beans in the code.
<bean id="b1" class="org.sssit.B"></bean> <bean id="b2" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="byName"></bean>
In this case, it will throw an exception.
In constructor auto-configuration mode, the Spring container injects dependencies through the most parameterized constructor.
If a class has3a constructor with zero parameters, one parameter, and two parameters, and then injects by calling the two-parameters constructor.
<bean id="b" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="constructor"></bean>
In the absence of auto-configuration mode, the Spring container will not inject dependencies through auto-configuration.
<bean id="b" class="org.sssit.B"></bean> <bean id="a" class="org.sssit.A" autowire="no"></bean>