标签:
public class AutoWiringDao { private String daoName; public void setDaoName(String daoName) { this.daoName = daoName; } public String getDaoName() { return daoName; } public void say(String word) { System.out.println(daoName + "===AutoWiringDao===" + word); } }
public class AutoWiringService { private AutoWiringDao autoWiringDao1; private AutoWiringService() { System.out.println("无参构造方法"); } private AutoWiringService(AutoWiringDao autoWiringDao3) { autoWiringDao1 = autoWiringDao3; System.out.println("有参构造方法===" + autoWiringDao1.getDaoName()); } public void setAutoWiringDao1(AutoWiringDao autoWiringDao) { autoWiringDao1 = autoWiringDao; } public void say(String word) { autoWiringDao1.say(word); } }
以上是所需要的类AutoWiringService与类AutoWiringDao。
1、byName
spring-autowiring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" default-autowire="byName" > <bean id="autoWiringDao" class="com.wangzhu.spring.autowiring.AutoWiringDao"> <property name="daoName" value="autoWiringDao"/> </bean> <bean id="autoWiringDao1" class="com.wangzhu.spring.autowiring.AutoWiringDao"> <property name="daoName" value="autoWiringDao1"/> </bean> <bean id="autoWiringDao2" class="com.wangzhu.spring.autowiring.AutoWiringDao"> <property name="daoName" value="autoWiringDao2"/> </bean> <bean class="com.wangzhu.spring.autowiring.AutoWiringDao"> <property name="daoName" value="null"/> </bean> <bean id="autoWiringService" class="com.wangzhu.spring.autowiring.AutoWiringService"></bean> </beans>
测试:
AutoWiringService service = getBean("autoWiringService");
service.say("this is a test autowire!");
输出:
无参构造方法 autoWiringDao1===AutoWiringDao===this is a test autowire!
备注:通过byName方法进行自动装配,
1)、需要无参构造方法;
2)、需要有同名的成员变量,以及该变量的set方法。
2、byType
只需要更改spring-autowiring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" default-autowire="byType" > <bean class="com.wangzhu.spring.autowiring.AutoWiringDao"> <property name="daoName" value="null"/> </bean> <bean id="autoWiringService" class="com.wangzhu.spring.autowiring.AutoWiringService"></bean> </beans>
输出:
无参构造方法
null===AutoWiringDao===this is a test autowire!
备注:通过byType方法进行自动装配,
1)、需要无参构成方法;
2)、同类型的Bean只能存在一个,否则将抛出异常;
3)、需要该成员变量的set方法,其变量名随意,故在spring-autowiring.xml中只需指定该Bean的class即可。
3、constructor
只需要更改spring-autowiring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" default-autowire="constructor" > <bean class="com.wangzhu.spring.autowiring.AutoWiringDao"> <property name="daoName" value="null"/> </bean> <bean id="autoWiringService" class="com.wangzhu.spring.autowiring.AutoWiringService"></bean> </beans>
输出:
有参构造方法===null
null===AutoWiringDao===this is a test autowire!
备注:通过constructor方法进行自动装配,
1)、需要对应的有参构造方法,若无则抛出异常,当然若有无参构造方法,则不会抛出异常;
2)、当只有一个同类型的成员变量的Bean时,直接指定该Bean的class即可;
3)、同有多个同类型的成员变量的Bean时,其会根据该Bean的名称与构造方法中的形参名进行比较,若不一样,则直接调用无参构造方法。
标签:
原文地址:http://www.cnblogs.com/xiaoxian1369/p/4320423.html