Spring学习记录(三)---bean自动装配autowire

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

autowire:

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

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

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

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

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

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

 package com.guigu.spring.autowire;
// Car类
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 + "]";
}
}
 package com.guigu.spring.autowire;
// Address 类
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 + "]";
}
}
 package com.guigu.spring.autowire;
// Person类
public class Person {
private String name;
private Car car;
private Address address; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [car=" + car + ", address=" + address + "]";
}
}
 public class Main {
// main
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("autowire.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person); }
}
 <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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="address" class="com.guigu.spring.autowire.Address"
p:city="BeiJing" p:street="HuiZhou"></bean>
<bean id="car" class="com.guigu.spring.autowire.Car"
p:brand="Audi" p:price="200000"></bean>
<!-- 手动装配
<bean id="person" class="com.guigu.spring.autowire.Person"
p:name="Tom" p:address-ref="address" p:car-ref="car"></bean>
-->
<!-- 自动装配 -->
<bean id="person" class="com.guigu.spring.autowire.Person"
p:name="Tom" autowire="byName"></bean> </beans>

结果会输出正确toString信息。

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

 <bean id="address" class="com.guigu.spring.autowire.Address"
p:city="BeiJing" p:street="HuiZhou"></bean> <!--等同于-->
<bean id="address" class="com.guigu.spring.autowire.Address">
<property name="city" value="BeiJing"></property>
<property name="street" value="HuiZhou"></property>
</bean>

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

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

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

     <bean id="address1" class="com.guigu.spring.autowire.Address"
p:city="BeiJing" p:street="HuiZhou"></bean>
<bean id="car1" class="com.guigu.spring.autowire.Car"
p:brand="Audi" p:price="200000"></bean>
<!-- 手动装配
<bean id="person" class="com.guigu.spring.autowire.Person"
p:name="Tom" p:address-ref="address" p:car-ref="car"></bean>
-->
<!-- 自动装配 -->
<bean id="person" class="com.guigu.spring.autowire.Person"
p:name="Tom" autowire="byType"></bean>

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

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

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

缺点:

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

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

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

上一篇:创建以 API 为中心的 Web 应用


下一篇:外网SSH访问内网LINUX的N种方法