自动装配
配置文件实现自动装配
- 以前的方式:
<?xml version="1.0" encoding="UTF-8"?>
<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="dog" scope="singleton" class="com.spring.pojo.Dog"></bean>
<bean id="cat" scope="singleton" class="com.spring.pojo.Cat"></bean>
<bean id="person" class="com.spring.pojo.Person">
<property name="dog" ref="dog"></property>
<property name="cat" ref="cat"></property>
</bean>
</beans>
现在的方式:
通过参数名装配
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- byName: 自动在容器上下文中查找,和自己set方法后的值相同对应的beanId -->
<bean id="dog" scope="singleton" class="com.spring.pojo.Dog"></bean>
<bean id="cat" scope="singleton" class="com.spring.pojo.Cat"></bean>
<bean id="person" class="com.spring.pojo.Person" autowire="byName">
</bean>
</beans>
通过参数类型装配
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- byType: 自自动在容器上下文中查找,和自己对象属性类型相同的bean -->
<bean scope="singleton" class="com.spring.pojo.Dog"></bean>
<bean scope="singleton" class="com.spring.pojo.Cat"></bean>
<bean id="person" class="com.spring.pojo.Person" autowire="byType">
</bean>
</beans>
- byName: 需要保证beanId唯一,并且该bean需要和对象set方法后的参数名称一致。
- byType:需要保证bean的class一致,并且该bean需要和对象set方法后的参数类型一致。
注解实现自动装配
@Autowired
Spring注解
配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="com.spring.pojo.Cat"></bean>
<bean class="com.spring.pojo.Dog"></bean>
<bean id="person" class="com.spring.pojo.Person" autowire="byType">
</bean>
</beans>
对象:
public class Person {
@Autowired
public Cat cat;
@Autowired
public Dog dog;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
}
@Autowired默认是根据byType的方式来实现自动装配的。如果我们对属性有两个类型相同的bean则会报错:
<bean id="dog1" class="com.spring.pojo.Dog"></bean>
<bean id="dog2" class="com.spring.pojo.Dog"></bean>
@Qualifier
Spring注解
public class Person {
@Autowired
public Cat cat;
@Autowired
@Qualifier(value = "dog1")
public Dog dog;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
}
如果我们对属性有两个类型相同的bean则会报错,@Qualifier注解帮助我们指定注入的对象。value属性也就是bean的id值。
@Nullable
表示字段可以为空。
@Resource
默认byName来注入,如果名称一致则根据byType来注入。
public class Person {
@Autowired
public Cat cat;
@Resource
public Dog dog;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
}
配置文件:
<bean id="dog" class="com.spring.pojo.Dog"></bean>
<bean id="dog1" class="com.spring.pojo.Dog"></bean>
<bean id="dog2" class="com.spring.pojo.Dog"></bean>
如果没有配置文件中的第一个,依旧会报错。当然我们可以根据注解加名称来指定注入对象。
@Resource(name = "dog1")
public Dog dog;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。