spring的自動裝配在工程中用得比較少,它沒有常規配置清晰明瞭。
//Address類spring
public class Address { private String city; private String street; }
//Car類ui
public class Car { private String brand; private double price; }
//Person類code
public class Person { private String name; private Address address; private Car car; }
配置文件ci
<bean id="address" class="com.test.autowired.Address" p:city="Beijing" p:street="Huilongguan"> </bean> <bean id="car" class="com.test.autowired.Car"> <property name="brand" value="Audi"></property> <property name="price"> <value>300000</value> </property> </bean> <!-- 根據 名字 進行自動裝配 --> <!--bean id="address" 要與Person中的setAddress(Address address)參數一致--> <bean id="person" class="com.test.autowired.Person" p:name="zhangsan" autowire="byName"></bean>