标签:
<bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"> </bean> <bean id="car" class="com.wul.spring.beans.autowire.Car" p:brand="car" p:price="300000"> </bean> <!-- 1.可以使用autowire属性指定自动装配方式,byName根据bean的名字和当前bean的setter风格的属性名进行自动装配 , 2.若有匹配的则进行自动装配,若没有匹配的,则不装配,值为null,不会出错. 3.上面的address和carid名必须和Person类中的属性名相同 --> <bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" autowire="byName"></bean>
<!-- byType根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中有1个以上的类型匹配的bean, 即其下假设还有一个Address类型的bean的话,其将无法分辨, 会出错 <bean id="address2" class="com.wul.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"> </bean> --> <bean id="person2" class="com.wul.spring.beans.autowire.Person" p:name="wul2" autowire="byType"></bean>3.construction(通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式将会很复杂,不推荐使用
<bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijiang^" p:street="WuDaoKou"> </bean> <!-- bean配置的继承:使用bean的parent属性指定继承哪个bean的配置 --> <bean id="address2" parent="address"></bean>
<bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijiang^" p:street="WuDaoKou"> </bean> <!-- bean配置的继承:使用bean的parent属性指定继承哪个bean的配置 --> <bean id="address2" p:city="Beijing" p:street="DaZhongSi" parent="address"></bean>address2里对应的bean中city和street属性会覆盖父类bean中的属性为Beijing和DaZhongSi。
<!-- 抽象bean:bean的abstract属性为true的bean,这样的bean不能被IOC容器实例化,只能用来被继承配置, 若某一个bean的class属性没有指定,则该bean必须是一个抽象bean --> <bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijiang^" p:street="WuDaoKou" abstract="true"> </bean> <bean id="address2" parent="address"></bean>
<bean id="address" p:city="Beijiang^" p:street="WuDaoKou" abstract="true"> </bean> <bean id="address2" class="com.wul.spring.beans.relations.Address2" parent="address"></bean>
<!-- 要求在配置Person时,必须有一个关联的car!换句话说,person这个bean依赖于Car这个bean --> <bean id="car" class="com.wul.spring.beans.autowire.Car" p:brand="Audi" p:price="300000"></bean> <bean id="addressdepend" class="com.wul.spring.beans.autowire.Address" p:city="shanghai" p:street="nanjinglu"></bean> <bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" p:address-ref="addressdepend" depends-on="car"></bean> </beans>
<bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" p:address-ref="addressdepend" p:car-ref="car" depends-on="car"></bean>
package com.wul.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 + "]"; } }
package com.wul.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 + "]"; } }
package com.wul.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 + "]"; } }
<?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:util="http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"> </bean> <bean id="car" class="com.wul.spring.beans.autowire.Car" p:brand="car" p:price="300000"> </bean> <!-- <bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" p:address-ref="address" p:car-ref="car"></bean> --> <!-- 可以使用autowire属性指定自动装配方式,byName根据bean的名字和当前bean的setter风格的属性名进行自动装配 , 若有匹配的则进行自动装配,若没有匹配的,则不装配,值为null,不会出错. 上面的address和carid名必须和Person类中的属性名相同--> <bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" autowire="byName"></bean> <!-- byType根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中有1个以上的类型匹配的bean, 即其下假设还有一个Address类型的bean的话,其将无法分辨, 会出错 <bean id="address2" class="com.wul.spring.beans.autowire.Address" p:city="Beijing" p:street="HuiLongGuan"> </bean> --> <bean id="person2" class="com.wul.spring.beans.autowire.Person" p:name="wul2" autowire="byType"></bean> </beans>
package com.wul.spring.beans.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) ctx.getBean("person"); System.out.println(person); Person person2 = (Person) ctx.getBean("person2"); System.out.println(person2); } }
<?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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- <bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijiang^" p:street="WuDaoKou"> </bean> bean配置的继承:使用bean的parent属性指定继承哪个bean的配置 <bean id="address2" p:city="Beijing" p:street="DaZhongSi" parent="address"></bean> --> <!-- 抽象bean:bean的abstract属性为true的bean,这样的bean不能被IOC容器实例化,只能用来被继承配置, 若某一个bean的class属性没有指定,则该bean必须是一个抽象bean <bean id="address" class="com.wul.spring.beans.autowire.Address" p:city="Beijiang^" p:street="WuDaoKou" abstract="true"> </bean> <bean id="address2" parent="address"></bean> --> <!-- 也可以忽略父Bean的class属性,让子Bean指定自己的类,而共享相同的属性配置,但此时abstract必须为true --> <bean id="address" p:city="Beijiang^" p:street="WuDaoKou" abstract="true"> </bean> <bean id="address2" class="com.wul.spring.beans.relations.Address2" parent="address"></bean> <!-- 要求在配置Person时,必须有一个关联的car!换句话说,person这个bean依赖于Car这个bean --> <bean id="car" class="com.wul.spring.beans.autowire.Car" p:brand="Audi" p:price="300000"></bean> <bean id="addressdepend" class="com.wul.spring.beans.autowire.Address" p:city="shanghai" p:street="nanjinglu"></bean> <!-- <bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" p:address-ref="addressdepend" depends-on="car"></bean> --> <bean id="person" class="com.wul.spring.beans.autowire.Person" p:name="wul" p:address-ref="addressdepend" p:car-ref="car" depends-on="car"></bean> </beans>
package com.wul.spring.beans.relations; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wul.spring.beans.autowire.Address; import com.wul.spring.beans.autowire.Car; import com.wul.spring.beans.autowire.Person; public class MainRelations { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); // Address address = (Address) ctx.getBean("address"); // System.out.println(address); // Address address2 = (Address) ctx.getBean("address2"); // System.out.println(address2); Address2 address3 = (Address2) ctx.getBean("address2"); System.out.println(address3); Person person = (Person) ctx.getBean("person"); System.out.println(person); } }
标签:
原文地址:http://blog.csdn.net/mrwuyi/article/details/51576603