Spring中bean有三种装配机制,分别是在xml中显式配置,在java中显式配置,隐式的bean发现机制和自动装配
Spring的自动装配需要通过两个操作来实现,一是组件扫描,spring会自动发现应用上下文中所创建的bean,二是自动装配,spring自动满足bean之间的依赖,也就是我们说的IoC/DI
新建实体类
package com.dream.pojo;
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
package com.dream.pojo;
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
package com.dream.pojo;
public class User {
private Cat cat;
private Dog dog;
private String str;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
编写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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.dream.pojo.Dog"/>
<bean id="cat" class="com.dream.pojo.Cat"/>
<bean id="user" class="com.dream.pojo.User">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
<property name="str" value="张三"/>
</bean>
</beans>
测试
import com.dream.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void testMethodAutowire() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
user.getCat().shout();
user.getDog().shout();
}
}
按名称自动装配(byName)
<bean id="dog" class="com.dream.pojo.Dog" />
<bean id="cat" class="com.dream.pojo.Cat"/>
<bean id="user" class="com.dream.pojo.User" autowire="byName">
<property name="str" value="张三"/>
</bean>
autowire="byName"会先查找其类中所有的set方法名,获得set后面且首字母变为小写的字符串,然后去Spring容器中查找是否有此字符串名称id的对象,如果有就取出注入
按类型自动装配(byType)
<bean id="doggg" class="com.dream.pojo.Dog" />
<bean id="cccat" class="com.dream.pojo.Cat"/>
<bean id="user" class="com.dream.pojo.User" autowire="byType">
<property name="str" value="张三"/>
</bean>
注意同一类型的对象,在spring容器中必须唯一
使用注解
在Spring配置文件中引入context文件头
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
开启属性注解支持
<context:annotation-config/>
@Autowired
按类型自动转配,不支持id匹配
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String str;
<bean id="dog" class="com.dream.pojo.Dog"/>
<bean id="dog2" class="com.dream.pojo.Dog"/>
<bean id="cat" class="com.dream.pojo.Cat"/>
<bean id="user" class="com.dream.pojo.User">
<property name="str" value="张三"/>
</bean>
@Autowired(required=false) 对象可以为null
@Autowired(required = false)
private Cat cat;
@Autowired加上@Qualifier可以先根据byName的方式自动装配,@Qualifier不能单独使用
<bean id="dog1" class="com.dream.pojo.Dog" />
<bean id="dog2" class="com.dream.pojo.Dog" />
<bean id="cat1" class="com.dream.pojo.Cat"/>
<bean id="cat2" class="com.dream.pojo.Cat"/>
<bean id="user" class="com.dream.pojo.User" >
<property name="str" value="张三"/>
</bean>
@Autowired
@Qualifier(value = "cat2")
private Cat cat;
@Autowired
@Qualifier(value = "dog1")
private Dog dog;
private String str;
@Resource
如有指定的name属性,先按该属性进行byName方式查找装配,其次再进行默认的byName方式进行装配,如果以上都不成功,则按byType的方式自动装配
<bean id="dog" class="com.dream.pojo.Dog" />
<bean id="dog2" class="com.dream.pojo.Dog" />
<bean id="cat2" class="com.dream.pojo.Cat"/>
<bean id="user" class="com.dream.pojo.User" >
<property name="str" value="张三"/>
</bean>
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
private String str;