标签:
XML配置里的Bean自动装配(三个来测试实现)
/**
* Person类
* */
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类
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类
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 + "]";
}
}
主程序Main
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans-autowire.xml");
Person person=(Person) ctx.getBean("person");
System.out.println(person);
}
}
bean-autowire.xml配置文件
<bean id="address2" class="com.wy.spring01.Address"
p:city="Beijing" p:street="HuiLongGang"></bean>
<bean id="car" class="com.wy.spring01.Car"
p:brand="Audi" p:price="30000"></bean>
<!-- 方式1
<bean id="person" class="com.wy.spring01.Person"
p:name="Tom" p:address-ref="address" p:car-ref="car"></bean>
-->
<!--方式2: 可以使用autowire属性指定自动配置方式 根据bean的名字和当前bean的setter风格的属性名进行自动装配;如有匹配则装配,若没有的则不装配-->
<bean id="person" class="com.wy.spring01.Person"
p:name="Tom" autowire="byName"></bean>
两种方式输出结果
按类型装配时,若IOC中有一个或者两个类型匹配的话,会出现指针异常
自动装配的缺点
autowire属性配置自动装配时,byName与byType只能二者选一,不能兼而有之
在bean中使用autowire这种自动配置时,必须所有的配置均使用这种形式,不能有的使用有的不用
bean之间的关系:继承与依赖(类同上)
主程序Main
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans-relation.xml");
Address address=(Address) ctx.getBean("address");
System.out.println(address);
address=(Address) ctx.getBean("address2");
System.out.println(address);
}
}
bean-relation.xml配置文件
<bean id="address" class="com.wy.autowire.Address"
p:city="BeiJing" p:street="WuDaoKou"></bean>
<bean id="address2" class="com.wy.autowire.Address"
p:city="BeiJing" p:street="DaZhongHua"></bean>
结果
与上面的xml配置达到同一种效果的配置方式--继承
<bean id="address" class="com.wy.autowire.Address"
p:city="BeiJing^" p:street="WuDaoKou"></bean>
<!-- bean的继承,使用bean的 parent属性继承哪个bean的配置-->
<bean id="address2" p:street="DaZhongHua" parent="address"></bean>
结果
Spring允许继承Bean的配置,被继承的bean称为父bean,继承的这个父bean的Bean称为子bean;子Bean从父Bean中继承配置,包括Bean的属性配置;父Bean可以作为配置的模板,也可以作为Bean的实例。若只想把父Bean作为模板,可以设置<bean>的abstract属性为true,这样Spring将不会实例化这个Bean;并不是<bean>元素里所有的属性都会被继承,比如:autowire,abstract等;也可以忽略父Bean的class属性,让子Bean指定自己的类,而共享相同的属性配置,但此时的abstract必须设定为true
依赖Bean配置
Spring允许用户通过depends-on属性设定Bean前置依赖Bean,前置依赖的Bean会在本Bean实例化之前创建好;如果前置依赖于多个Bean,则可以通过逗号,空格或的方式配置Bean的名称
bean-depend-on.xml
<!-- depend-on依赖 -->
<bean id="person" class="com.wy.autowire.Person"
p:name="Jay" p:address-ref="address"></bean>
depend-on 要求在配置Person时必须有个关联的Car,也就是我依赖于这个Car这个Bean的配置
<bean id="person" class="com.wy.autowire.Person"
p:name="Jay" p:address-ref="address" depends-on="car"></bean>
<bean id="car" class="com.wy.autowire.Car"
p:brand="Audi" p:price="250000"></bean>
结果
标签:
原文地址:http://www.cnblogs.com/sinon/p/5850437.html