从头认识Spring-2.1 自动装配-constructor
Posted 李灵晖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识Spring-2.1 自动装配-constructor相关的知识,希望对你有一定的参考价值。
这一章节我们来讨论一下自动装配的第三种方式-constructor
1.domain
蛋糕类:(不变)
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4;
public class Cake {
private String name = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
厨师类:(由于是使用构造器方式,因此下面的代码需要改变一下)
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4;
public class Chief {
private Cake cake = null;
private String name = "";
public Chief(Cake cake) {
this.cake = cake;
}
public String getName() {
return name;
}
public Cake makeOneCake() {
System.out.println(getName() + " make " + cake.getName());
return cake;
}
public void setName(String name) {
this.name = name;
}
}
2.测试类:(不变)
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_4/ApplicationContext-test.xml" })
public class ChiefTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testChief() {
Chief jack = (Chief) applicationContext.getBean("jack");
jack.makeOneCake();
}
}
3.配置文件(重点)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="blueberryCheeseCake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="blueberryCheeseCake" scope="prototype" />
<bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Chief"
p:name="jack" autowire="constructor" />
</beans>
根据上面的配置文件测试输出:
jack make blueberryCheeseCake
在上面的配置文件里面我们需要注意的地方:
在jack里面,没有了cake属性的注入,但是增加了autowire="constructor"这一句。
autowire="constructor":表示当Bean的类型跟构造器注入的类型相一致、或者Bean的id跟构造器注入的名称相一致的时候,即可注入,也就是,当jack的构造器需要注入Cake类型,而在配置文件里面有一个bean的类型是Cake,那么,系统就会自动的把bean注入到属性里面去,或者当jack的构造器需要注入的是cake名称的对象,而在配置文件里面有那么一个bean,系统就可以自动注入。
但是这里有一个问题,当配置文件里面有多个Cake,这时候系统会抛异常
下面,我们将给出不同的配置文件以及输出结果,以供参考:
(1)当有两个Cake,但是其中一个bean的id跟注入的名字相同
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="cake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="cake" scope="prototype" />
<bean id="blueberryCheeseCake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="blueberryCheeseCake" scope="prototype" />
<bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Chief"
p:name="jack" autowire="constructor" />
</beans>
根据上面的配置文件测试输出:
jack make cake
(2)当没有相同名称的时候,但是又存在两个Cake的Bean,就会抛异常
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="cake2"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="cake2" scope="prototype" />
<bean id="blueberryCheeseCake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="blueberryCheeseCake" scope="prototype" />
<bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Chief"
p:name="jack" autowire="constructor" />
</beans>
我们把上面的cake的名称改成cake2以示区别
根据上面的配置文件测试输出:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake] is defined: expected single matching bean but found 2: [cake2, blueberryCheeseCake]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:796)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
... 40 more
(3)为了解决上面的问题,我们引入之前一章节提到的autowire-candidate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="cake2"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="cake2" scope="prototype" autowire-candidate="false"/>
<bean id="blueberryCheeseCake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Cake"
p:name="blueberryCheeseCake" scope="prototype" />
<bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_4.Chief"
p:name="jack" autowire="constructor" />
</beans>
我们在上面的cake2里面加上了autowire-candidate属性,自我排除候选
根据上面的配置文件测试输出:
jack make blueberryCheeseCake
总结:这一章节主要介绍了autowire属性里面的constructor选项的使用以及相应的注意点。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
以上是关于从头认识Spring-2.1 自动装配-constructor的主要内容,如果未能解决你的问题,请参考以下文章
从头认识Spring-2.3 注解装配-@autowired-required