Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式
byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.
constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用
我们现在来看一个例子:
定义三个类,其中PersonBean包含了AddressBean和CarBean类型的属性。
package com.happyBKs.autowire; public class AddressBean { String city; String street; public AddressBean() { super(); // TODO Auto-generated constructor stub } public AddressBean(String city, String street) { super(); this.city = city; this.street = 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 "AddressBean [city=" + city + ", street=" + street + "]"; } }
package com.happyBKs.autowire; public class CarBean { String brand; 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; } public CarBean(String brand, double price) { super(); this.brand = brand; this.price = price; } public CarBean() { super(); } @Override public String toString() { return "CarBean [brand=" + brand + ", price=" + price + "]"; } }
package com.happyBKs.autowire; public class PersonBean { String name; AddressBean address; CarBean car; public String getName() { return name; } public void setName(String name) { this.name = name; } public AddressBean getAddress() { return address; } public void setAddress(AddressBean address) { this.address = address; } public CarBean getCar() { return car; } public void setCar(CarBean car) { this.car = car; } @Override public String toString() { return "PersonBean [name=" + name + ", address=" + address + ", car=" + car + "]"; } public PersonBean() { super(); // TODO Auto-generated constructor stub } public PersonBean(String name, AddressBean address, CarBean car) { super(); this.name = name; this.address = address; this.car = car; } }
我们配置IOC容器的配置文件:(记得把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="addressId" class="com.happyBKs.autowire.AddressBean" p:city="Shanghai" p:street="Nanjing Road" /> <bean id="carId" class="com.happyBKs.autowire.CarBean" p:brand="BM" p:price="800000" /> <bean id="personId" class="com.happyBKs.autowire.PersonBean" p:name="happyBKs" p:address-ref="addressId" p:car-ref="carId" /> </beans>
然后我们测试代码:
@Test public void testAuto1() { ApplicationContext ac=new ClassPathXmlApplicationContext("beans-autowire.xml"); PersonBean pb=(PersonBean) ac.getBean("personId"); System.out.println(pb); }
输出:
PersonBean [name=happyBKs, address=AddressBean [city=Shanghai, street=Nanjing Road], car=CarBean [brand=BM, price=800000.0]]
现在,我们设想一种场景,PersonBean有好多好多好多属性是各种bean类型(不止这两个),并且现在有好多好多PersonBean,一个个写不是太繁琐了吗?如果PersonBean的属性名与我配置的相应的bean的名称一样,我们是否可以将它们自动关联起来呢?答案就是自动装配autowire了。
这里autowire可以作为bean标签大的属性进行设置,有byName和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="addressId" class="com.happyBKs.autowire.AddressBean" p:city="Shanghai" p:street="Nanjing Road" /> <bean id="carId" class="com.happyBKs.autowire.CarBean" p:brand="BM" p:price="800000" /> <bean id="personId" class="com.happyBKs.autowire.PersonBean" p:name="happyBKs" p:address-ref="addressId" p:car-ref="carId" /> <!-- 可以使用autowire属性指定自动装配的方式--> <!-- byName根据bean的名字和当前bean的setter风格的属性名进行自动装配:若有匹配的则进行自动装配;若无匹配的则不装配,为null --> <bean id="address" class="com.happyBKs.autowire.AddressBean" p:city="Shanghai" p:street="Huaihai Road" /> <bean id="car" class="com.happyBKs.autowire.CarBean" p:brand="Audi" p:price="800000" /> <bean id="personId2" class="com.happyBKs.autowire.PersonBean" p:name="happyBKs" autowire="byName" /> </beans>
测试程序:
@Test public void testAuto2() { ApplicationContext ac=new ClassPathXmlApplicationContext("beans-autowire.xml"); PersonBean pb=(PersonBean) ac.getBean("personId2"); System.out.println(pb); }
输出:
PersonBean [name=happyBKs, address=AddressBean [city=Shanghai, street=Huaihai Road], car=CarBean [brand=Audi, price=800000.0]]
注意:这里如果某个名字没有匹配上,那么bean对象相应的属性为null。
我们再尝试使用byType。这里需要注意的是,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="addressId" class="com.happyBKs.autowire.AddressBean" p:city="Shanghai" p:street="Nanjing Road" /> <bean id="carId" class="com.happyBKs.autowire.CarBean" p:brand="BM" p:price="800000" /> <bean id="personId" class="com.happyBKs.autowire.PersonBean" p:name="happyBKs" p:address-ref="addressId" p:car-ref="carId" /> <!-- 可以使用autowire属性指定自动装配的方式--> <!-- byName根据bean的名字和当前bean的setter风格的属性名进行自动装配:若有匹配的则进行自动装配;若无匹配的则不装配,为null --> <!-- byType根据 bean的类型和当前bean的属性的类型自动装配,若IOC容器中有一个以上的类型匹配,则抛异常--> <bean id="personId3" class="com.happyBKs.autowire.PersonBean" p:name="happyBKs" autowire="byType" /> </beans>
测试程序:
@Test public void testAuto3() { ApplicationContext ac=new ClassPathXmlApplicationContext("beans-autowire.xml"); PersonBean pb=(PersonBean) ac.getBean("personId3"); System.out.println(pb); }
输出结果:
PersonBean [name=happyBKs, address=AddressBean [city=Shanghai, street=Nanjing Road], car=CarBean [brand=BM, price=800000.0]]
Bean 自动装配的缺点
在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了.
autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.
一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些