1.Bean的自动装配
- 自动装配是Spring满足bean依赖一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配方式
- 在xml中显式的配置
- 在java中显式配置
- 隐式的自动装配bean[重要]
示例
(1)创建实体类
public class Cat {
public String cat;
public void shut(){
System.out.println("miao");
}
@Override
public String toString() {
return "Cat{" +
"cat='" + cat + '\'' +
'}';
}
}
public class Dog {
public String dog;
public void shut(){
System.out.println("wang");
}
@Override
public String toString() {
return "Dog{" +
"dog='" + dog + '\'' +
'}';
}
}
public class People {
private Cat cat;
private Dog dog;
private String name;
public People() {
}
public People(Cat cat, Dog dog, String name) {
this.cat = cat;
this.dog = dog;
this.name = name;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
(2)编写配置文件
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.xupt.pojo.Cat"/>
<bean id="dog" class="com.xupt.pojo.Dog"/>
<bean id="people" class="com.xupt.pojo.People" autowire="byType" p:name="zhagns"/>
<!--byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
有<context:annotation-config/>可以省略people后的autowire
-->
<bean id="people2" class="com.xupt.pojo.People" p:name="didi" autowire="byName"/>
</beans>
小结
- byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的Set方法的值一致
- bytype的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
2.使用注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了!
要使用注解须知:
- 导入约束:context约束
- 配置注解的支持:context:annotation-config/
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="cat" class="com.xupt.pojo.Cat"/>
<bean id="dog" class="com.xupt.pojo.Dog"/>
<!--指定要扫描的包,这个包下的注解才会生效-->
<context:component-scan base-package="com.kuang.pojo"/>
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写set方法了,前提是自动装配的属性在IOC(Spring)容器中存在,先找bytype不符合的话找byname!
如果@Autowired自动装配的环境比较复杂,自动那个装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifler(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!
@Autowired和@Qualifler是Spring中的,@Resource是java注解
@Resource自动装配通过名字,名字不行的话通过类型