introduce
- Autowiring is a way for Spring to satisfy bean dependencies
- The spring context will automatically find and automatically assemble the bean
- There are three ways of automatic assembly in spring
1. Configuration displayed in xml
2. Configuration shown in Java
3. Implicit automatic assembly bean (important)
Here we mainly talk about the third type: automated assembly bean s.
Spring's automatic assembly needs to be implemented from two perspectives, or two operations:
Component scanning (component scanning): spring will automatically discover the bean s created in the application context;
Autowiring: spring automatically satisfies the dependencies between bean s, which is what we call IoC/DI;
The combination of component scanning and autowiring is powerful enough to minimize the configuration shown.
test
Environment collocation
- Requirements: One person has two pets! and call
Create three entity classes
- cat
public class Cat { public void shout(){ System.out.println("miao~~"); } }
- dog
public class Dog { public void shout(){ System.out.println("wang~~"); } }
- people
public class People { private Dog dog; private Cat cat; private String name; public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "People{" + "dog=" + dog + ", cat=" + cat + ", name='" + name + '\'' + '}'; } }
beans.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.wxt.pojo.Cat"></bean> <bean id="dog" class="com.wxt.pojo.Dog"></bean> <bean id="people" class="com.wxt.pojo.People" > <property name="dog" ref="dog"></property> <property name="cat" ref="cat"></property> <property name="name" value="Xiao Wu" ></property> </bean> </beans>
result
public class MtTest { @Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); people.getCat().shout(); people.getDog().shout(); } }
byname autowiring
Change the above beans.xml
Modify the bean configuration and add an attribute autowire="byName"
<bean id="cat" class="com.wxt.pojo.Cat"></bean> <bean id="dog" class="com.wxt.pojo.Dog"></bean> <bean id="people" class="com.wxt.pojo.People" autowire="byName"> <property name="name" value="Xiao Wu" ></property> </bean>
result
- Summary: We can find that the result is the same as above. So we can add an attribute autowire="byName" to run without affecting the result.
Modify beans.xml again
We change the bean id of cat to catXXX`
<bean id="cat111" class="com.wxt.pojo.Cat"></bean> <bean id="dog" class="com.wxt.pojo.Dog"></bean> <bean id="people" class="com.wxt.pojo.People" autowire="byName"> <property name="name" value="Xiao Wu" ></property> </bean>
run:
summary:
1. A null pointer java.lang.NullPointerException is reported during execution.
2. The entity class is setCat and the id is catXXX is different
3. It can be concluded that byname automatic assembly is searched according to the id of the bean
byname: It will automatically look up in the container context, and the beanID corresponding to the value behind the set method of its own object
4. The reason for the error: because the set method cannot be found according to the byName rule, the real setCat is not executed, and the object is not initialized, so a null pointer error will be reported when calling.
Modify beans.xml
Add a same id
You will find that idea directly reports an error:
- When bynam, it is necessary to ensure that the id of all beans is unique, and this bean needs to have the same value as the set method of the automatically injected property!
When a bean node has the property of autowire byName.
1. It will search for all the set method names in its class, such as setCat, and obtain a string with the set removed and the first letter lowercase, that is, cat.
2. Go to the spring container to find if there is an object with this string name id.
- If there is, take out the injection; if not, report a null pointer exception.
byType
test
Change beans.xml directly
autowire byType
<bean id="catXXX" class="com.wxt.pojo.Cat"></bean> <bean id="dog" class="com.wxt.pojo.Dog"></bean> <bean id="people" class="com.wxt.pojo.People" autowire="byType"> <property name="name" value="Xiao Wu" ></property> </bean>
run:
success!
Summary: 1.bytype is not matched according to the beanID corresponding to the value behind the set method of its own object
2. It will automatically find bean s of the same type as its own object properties in the container context
Suppose we have two dog s
It can be found that the idea directly reports an error: the bytype type must be globally unique
Summarize
- When bynam, it is necessary to ensure that the id of all beans is unique, and this bean needs to have the same value as the set method of the automatically injected property!
- When bytype, it is necessary to ensure that all class es are unique, and this bean needs to be consistent with the automatically injected attribute type