自动装配与兼容性
程序员文章站
2022-07-13 10:11:03
...
自动装配的概念
- 1)手动装配:以value或ref的方式明确指定属性值都是手动装配。
- 2)自动装配:根据指定的装配规则,不需要明确指定,Spring自动将匹配的属性值注入bean中。
写几个类方便后续的测试:
public class Emp {
private Integer eid;
private String ename;
private Car car;
private Dept dept;
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"eid=" + eid +
", ename='" + ename + '\'' +
", car=" + car +
", dept=" + dept +
'}';
}
}
public class Dept {
private Integer did;
private String dname;
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"did=" + did +
", dname='" + dname + '\'' +
'}';
}
}
public class Car {
private Integer cid;
private String cname;
@Override
public String toString() {
return "Car{" +
"cid=" + cid +
", cname='" + cname + '\'' +
'}';
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
这里我们写一个配置文件并测试手动装配:
<?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="emp" class="com.atguigu.spring.day02_Spring.Auto.Emp" >
<property name="eid" value="1001"/>
<property name="ename" value="亚索"/>
<property name="car" ref="car"/>
<property name="dept" ref="dept"/>
</bean>
<bean id="car" class="com.atguigu.spring.day02_Spring.Auto.Car">
<property name="cid" value="10010"/>
<property name="cname" value="兰博基尼"/>
</bean>
<bean id="dept" class="com.atguigu.spring.day02_Spring.Auto.Dept">
<property name="did" value="10086"/>
<property name="dname" value="管理组"/>
</bean>
</beans>
测试:
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("auto.xml");
Emp emp = ac.getBean("emp", Emp.class);
System.out.println(emp);
}
}
输出结果:
装配模式
- 1)根据类型自动装配:将类型匹配的bean作为属性注入到另一个bean中。若IOC容器中有多个与目标bean类型一致的bean,Spring将无法判定哪个bean最合适该属性,所以不能执行自动装配。向上兼容,可以赋值给其父类或者接口。
<?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="emp" class="com.atguigu.spring.day02_Spring.Auto.Emp" autowire="byType">
<property name="eid" value="1001"/>
<property name="ename" value="亚索"/>
</bean>
<bean id="car1" class="com.atguigu.spring.day02_Spring.Auto.Car">
<property name="cid" value="10010"/>
<property name="cname" value="兰博基尼"/>
</bean>
<bean id="dept1" class="com.atguigu.spring.day02_Spring.Auto.Dept">
<property name="did" value="10086"/>
<property name="dname" value="管理组"/>
</bean>
</beans>
这里是byType,目标的bean的id名称可不与属性名一致
- 2)根据名称自动装配:必须将目标bean的名称和属性名设置的完全相同
<?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="emp" class="com.atguigu.spring.day02_Spring.Auto.Emp" autowire="byName">
<property name="eid" value="1001"/>
<property name="ename" value="亚索"/>
</bean>
<bean id="car" class="com.atguigu.spring.day02_Spring.Auto.Car">
<property name="cid" value="10010"/>
<property name="cname" value="兰博基尼"/>
</bean>
<bean id="dept" class="com.atguigu.spring.day02_Spring.Auto.Dept">
<property name="did" value="10086"/>
<property name="dname" value="管理组"/>
</bean>
</beans>
测试:
- 3)通过构造器自动装配:当bean中存在多个构造器时,此种自动装配方式将会很复杂。不推荐使用。
相对于使用注解的方式实现的自动装配,在XML文档中进行的自动装配略显笨拙,在项目中更多的使用注解的方式实现。