Spring IOC 容器可以自动装配Bean。需要做的仅是在<bean>的autowire属性里指定自动装配的模式
byType(根据类型自动装配):若IOC容器有多个与目标Bean类型一致的Bean.在这种情况下,Spring将无法判定哪个Bean最合适该属性,所以不能执行自动装配。
byName(根据名称自动装配):必须将目标Bean的名称和属性名设置的完全相同。
constructor(通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式将会很复杂。不推荐使用。
autowire.xml配置文件:是使用了byName去自动装配的。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="car" class="autowire.Car"> <property name="carName" value="baoma"></property> <property name="carPrice" value="10000.00"></property> </bean> <bean id="address" class="autowire.Address"> <property name="city" value="beijing"></property> <property name="street" value="zhonghuajie"></property> </bean> <!--使用autowire自动装配,而与它伴随的是p命名--> <bean id="person" class="autowire.Person" p:name="chen" p:age="23" autowire="byName"></bean> </beans>
package autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
collection.Person person = (collection.Person) applicationContext.getBean ("person");
System.out.println (person);
}
}
Address类的文件
package autowire; public class Address { public String city; public 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类的文件
package autowire; public class Car { public String carName; public double carPrice; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public double getCarPrice() { return carPrice; } public void setCarPrice(double carPrice) { this.carPrice = carPrice; } @Override public String toString() { return "Car{" + "carName='" + carName + '\'' + ", carPrice=" + carPrice + '}'; } }
Main类文件:
package autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml"); collection.Person person = (collection.Person) applicationContext.getBean ("person"); System.out.println (person); } }
输出结果:
collection.Person{name='chen', age=56, cars=[Car{name='baoma', num=12, color='red', price=0.0}, Car{name='bentian', num=13, color='blue', price=0.0}]}
byType(根据类型自动装配):
autowire.xml的配置文件:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="car" class="autowire.Car"> <property name="carName" value="baoma"></property> <property name="carPrice" value="10000.00"></property> </bean> <bean id="car1" class="autowire.Car"> <property name="carName" value="bentian"></property> <property name="carPrice" value="2000000.00"></property> </bean> <bean id="address" class="autowire.Address"> <property name="city" value="beijing"></property> <property name="street" value="zhonghuajie"></property> </bean> <!--使用autowire自动装配,而与它伴随的是p命名--> <!--<bean id="person" class="autowire.Person" p:name="chen" p:age="23" autowire="byName"></bean>--> <bean id="person" class="autowire.Person" p:name="chen" p:age="32" autowire="byType"></bean> </beans>
其它文件都是与上面一样的。
总结:
可以使用autowire属性指定自动装配的方式
byName 根据bean的名字和当前bean的setter风格的属性名进行自动装配,若有匹配,则进行自动匹配。若没有匹配的,则不装配。
byType 根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中有1个以上的类型匹配的bean,则抛异常。会出现装配的bean不唯一的错误。