原文地址:http://blog.csdn.net/conglinyu/article/details/63684957
Spring 自动装配
通过配置default-autowire 属性,Spring IOC 容器可以自动为程序注入bean;默认是no,不启用自动装配;
default-autowire 的类型有byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
【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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byName">
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <!-- 自动装配 byName 来找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奥拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
实体类
- public class Car {
- private int id;
- private String carName;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getCarName() {
- return carName;
- }
- public void setCarName(String carName) {
- this.carName = carName;
- }
- }
- public class Employee {
- private int id;
- private String name;
- private String sex;
- private Car car;
- public Employee() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Employee(int id, String name, String sex) {
- super();
- this.id = id;
- this.name = name;
- this.sex = sex;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public Car getCar() {
- return car;
- }
- public void setCar(Car car) {
- this.car = car;
- }
- }
测试:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李经理");//在xml中属性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
【byType】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byType">
- <!-- 自动装配 byType 有两个相同类型会报错 -->
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <!-- 自动装配 byType 来找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奥拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
测试:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李经理");//在xml中属性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
运行:
【default-autowire="constructor"】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="constructor">
- <!-- 自动装配 constructor 需要写单参构造方法 -->
- <bean id="car3" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
- 自动装配 constructor 需要写单参构造方法
不写的话会报告错误