Spring IOC容器可以自动装配Bean,需要做的仅仅是在的autowire属性里指定自动装配的模式。
byType(根据类型自动装配):若IOC容器中有多个与目标Bean类型一致的Bean。在这种情况下,Spring将无法判定哪个bean最适合该属性,所以不能执行自动装配。
byName(根据名称自动装配):必须将目标Bean的名称和属性名设置的完全相同。
constructor(通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式将会很复杂。不推荐使用。
举例说明,在src目录下新建com.spring.beans.autowire包,新建一个Person.java类:
package com.spring.beans.autowire;
public class Person {
private String name;
private Address address;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", address=" + address + ", car=" + car + "]";
}
}
新建一个Address.java类
package com.spring.beans.autowire;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
新建一个Car.java类:
package com.spring.beans.autowire;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
在src目录下新建一个beans-autowire.xml配置文件。引入p命名空间,先进性测试:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address1" class="com.spring.beans.autowire.Address" p:city="Beijing" p:street="Huilongguan"></bean>
<bean id="car2" class="com.spring.beans.autowire.Car" p:brand="Audi" p:price="3000"></bean>
<bean id="person" class="com.spring.beans.autowire.Person" p:name="Tom" p:car-ref="car2" p:address-ref="address1"></bean>
<!-- <bean id="person" class="com.spring.beans.autowire.Person" p:name="Tom" autowire="byType"></bean> -->
</beans>
同样在com.spring.beans.autowire包下新建一个Main.java:
package com.spring.beans.autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-autowire.xml");
Person person=(Person)ctx.getBean("person");
System.out.println(person);
}
}
测试结果为:
下面测试进行自动装配,因为Person.java中有三个属性name、address、car,其中后两个是引用类型。如果采用自动装配,则beans-autowire.xml配置文件中改为成下面这种形式:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.spring.beans.autowire.Address" p:city="Beijing" p:street="Huilongguan"></bean>
<bean id="car" class="com.spring.beans.autowire.Car" p:brand="Audi" p:price="3000"></bean>
<!-- <bean id="person" class="com.spring.beans.autowire.Person" p:name="Tom" p:car-ref="car2" p:address-ref="address1"></bean> -->
<bean id="person" class="com.spring.beans.autowire.Person" p:name="Tom" autowire="byName"></bean>
</beans>
除了name属性单独赋值之外,其他采用自动装配的形式,上述采用的byName的方式,因为Person.java中有car和address两个引用的属性,所以必须保证前面两个bean的id必须是car和address,否则找不到,如下所示,如果将第二个bean的id设为car1或者Car:
则输出以下信息:
下面测试采用byType的方式,
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address1" class="com.spring.beans.autowire.Address" p:city="Beijing" p:street="Huilongguan"></bean>
<bean id="car2" class="com.spring.beans.autowire.Car" p:brand="Audi" p:price="3000"></bean>
<!-- <bean id="person" class="com.spring.beans.autowire.Person" p:name="Tom" p:car-ref="car2" p:address-ref="address1"></bean> -->
<bean id="person" class="com.spring.beans.autowire.Person" p:name="Tom" autowire="byType"></bean>
</beans>
如果采用byType的方式,则前面两个bean的id无论是什么都能装配的上,因为是根据类型来装配的。输出结果:
采用byType时,如果IOC容器中某个类型有多于一个的bean,则系统不知道匹配哪个。
使用自动装配方式时,必须为所有的引用类型进行自动装配,无法部分使用。而且要么使用byType,要么使用byName,两者不能同时使用。在实际的项目中很少使用自动装配的方式。