Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref

autowire:

  no ---默认情况,不自动装配,通过ref手动引用

  byName---根据名字自动装配

  byType---根据类型自动装配

  constructor---根据构造函数参数的数据类型,进行byType模式的自动装配。 (麻烦,不推荐)

  autodetect--- 首先尝试使用constructor进行自动装配,如果失败,再使用byType进行自动装配(麻烦,不推荐)

看一个例子:Person类有name属性、Car对象属性、Address对象属性

 1 package com.guigu.spring.autowire;
 2  // Car类
 3 public class Car {
 4     private String brand;
 5     private double price;
 6     public String getBrand() {
 7         return brand;
 8     }
 9     public void setBrand(String brand) {
10         this.brand = brand;
11     }
12     public double getPrice() {
13         return price;
14     }
15     public void setPrice(double price) {
16         this.price = price;
17     }
18     @Override
19     public String toString() {
20         return "Car [brand=" + brand + ", price=" + price + "]";
21     }
22 }

 

 1 package com.guigu.spring.autowire;
 2  // Address 类
 3 public class Address {
 4     private String city;
 5     private String street;
 6     public String getCity() {
 7         return city;
 8     }
 9     public void setCity(String city) {
10         this.city = city;
11     }
12     public String getStreet() {
13         return street;
14     }
15     public void setStreet(String street) {
16         this.street = street;
17     }
18     @Override
19     public String toString() {
20         return "Address [city=" + city + ", street=" + street + "]";
21     }
22 }

 

 

 1 package com.guigu.spring.autowire;
 2  // Person类
 3 public class Person {
 4     private String name;
 5     private Car car;
 6     private Address address;
 7     
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public Car getCar() {
15         return car;
16     }
17     public void setCar(Car car) {
18         this.car = car;
19     }
20     public Address getAddress() {
21         return address;
22     }
23     public void setAddress(Address address) {
24         this.address = address;
25     }
26     @Override
27     public String toString() {
28         return "Person [car=" + car + ", address=" + address + "]";
29     }
30 }

 

 

 1 public class Main {
 2 // main
 3     public static void main(String[] args) {
 4     
 5         ApplicationContext ctx = new ClassPathXmlApplicationContext("autowire.xml");
 6         Person person = (Person) ctx.getBean("person"); 
 7         System.out.println(person);
 8         
 9     }
10 }

 

 

 1 <beans  
 2 xmlns="http://www.springframework.org/schema/beans"  
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4 xmlns:context="http://www.springframework.org/schema/context"  
 5 xsi:schemaLocation="  
 6 http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 7 http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
 8     <bean id="address" class="com.guigu.spring.autowire.Address"
 9     p:city="BeiJing" p:street="HuiZhou"></bean>
10     <bean id="car" class="com.guigu.spring.autowire.Car"
11     p:brand="Audi" p:price="200000"></bean>
12     <!-- 手动装配 
13     <bean id="person" class="com.guigu.spring.autowire.Person"
14     p:name="Tom" p:address-ref="address" p:car-ref="car"></bean>
15     -->
16     <!-- 自动装配 -->
17     <bean id="person" class="com.guigu.spring.autowire.Person"
18     p:name="Tom" autowire="byName"></bean>
19 
20 </beans>  

 

结果会输出正确toString信息。

 

补充一下 P:city ,这是用p命名空间,配置属性更简便

1 <bean id="address" class="com.guigu.spring.autowire.Address"
2     p:city="BeiJing" p:street="HuiZhou"></bean>
3 
4 <!--等同于-->
5 <bean id="address" class="com.guigu.spring.autowire.Address">
6 <property name="city" value="BeiJing"></property>
7 <property name="street" value="HuiZhou"></property>
8 </bean>

 

继续说自动装配,autowire="byName"达到了和手动装配一样的效果。

它的匹配方法是依据person类有两个属性,一个是address,一个是car,正好有两个bean名字就是对应两个属性名,就会自动匹配上了。

缺点是当bean的id变成address1,返回null,因为名字不匹配。。

 1     <bean id="address1" class="com.guigu.spring.autowire.Address"
 2     p:city="BeiJing" p:street="HuiZhou"></bean>
 3     <bean id="car1" class="com.guigu.spring.autowire.Car"
 4     p:brand="Audi" p:price="200000"></bean>
 5     <!-- 手动装配 
 6     <bean id="person" class="com.guigu.spring.autowire.Person"
 7     p:name="Tom" p:address-ref="address" p:car-ref="car"></bean>
 8     -->
 9     <!-- 自动装配 -->
10     <bean id="person" class="com.guigu.spring.autowire.Person"
11     p:name="Tom" autowire="byType"></bean>

 

这样,即使名字不能匹配,但可以用类型匹配。person类有两个属性,一个是Address类,一个是Car类,正好前面两个bean是这样类型的,就匹配上了。不管它们叫什么名字

缺点是,若前面有多个Car类,那它就不知道匹配谁了,会报异常。。如下

 1     <bean id="address" class="com.guigu.spring.autowire.Address"
 2     p:city="BeiJing" p:street="HuiZhou"></bean>
 3     <bean id="car" class="com.guigu.spring.autowire.Car"
 4     p:brand="Audi" p:price="200000"></bean>
 5     <bean id="car1" class="com.guigu.spring.autowire.Car"
 6     p:brand="Baoma" p:price="300000"></bean>
 7 
 8     <!-- 自动装配 -->
 9     <bean id="person" class="com.guigu.spring.autowire.Person"
10     p:name="Tom" autowire="byType"></bean>

 

 

缺点:

1、autowire属性将自动装配bean的所有属性,若只希望装配个别属性时,显得不那么灵活。

2、一般用byName或byType,不能二者一起用

 

在实际中较少用,在一些整合框架中用到。

 

posted on 2016-08-23 13:21  ooooevan  阅读(836)  评论(0编辑  收藏  举报