从头认识Spring-2.1 自动装配(2)-byType(1)

来源:互联网 发布:sm公司知乎 编辑:程序博客网 时间:2024/06/16 17:27

这一章节我们来讨论一下自动装配的第二种方式-byType

1.domain

蛋糕类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2;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_2;public class Chief {private Cake cake = null;private String name = "";public Cake getCake() {return cake;}public String getName() {return name;}public Cake makeOneCake() {System.out.println(getName() + " make " + getCake().getName());return cake;}public void setCake(Cake cake) {this.cake = cake;}public void setName(String name) {this.name = name;}}


 

2.测试类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2;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_2/ApplicationContext-test.xml" })public class ChiefTest {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testChief() {Chief jack = (Chief) applicationContext.getBean("jack");jack.makeOneCake();Chief rose = (Chief) applicationContext.getBean("rose");rose.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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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_2.Cake"p:name="blueberryCheeseCake" scope="prototype" /><bean id="jack"class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2.Chief"p:name="jack"><property name="cake" ref="blueberryCheeseCake" /></bean><bean id="rose"class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2.Chief"autowire="byType" p:name="rose" /></beans>


在上面的配置文件里面我们需要注意的地方:

在rose里面,没有了cake属性的注入,但是增加了autowire="byType"这一句。

autowire="byType":表示当Bean的类型跟注入属性的类型相一致的时候,即可注入,也就是,当rose里面有一个属性需要注入Cake类型,在配置文件里面只需要有一个bean的类型是Cake,那么,系统就会自动的把bean注入到属性里面去

但是这里有一个问题,当配置文件里面有多个Cake,这时候系统会抛异常,因为不知道注入那个对象


下面是错误的配置文件:

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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_2.Cake"p:name="blueberryCheeseCake" scope="prototype" /><bean id="cake"class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2.Cake"p:name="cake" scope="prototype" /><bean id="jack"class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2.Chief"p:name="jack"><property name="cake" ref="cake" /></bean><bean id="rose"class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_2.Chief"autowire="byType" p:name="rose" /></beans>


 

测试输出:

jack make blueberryCheeseCake
rose make blueberryCheeseCake

 

总结:这一章节主要介绍了autowire属性里面的byType选项的使用。

 

目录:http://blog.csdn.net/raylee2007/article/details/50611627 

 

我的github:https://github.com/raylee2015/my_new_spring

 

0 0