从头认识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
- 从头认识Spring-2.1 自动装配(2)-byType(1)
- 从头认识Spring-2.1 自动装配(2)-byType(2)
- 从头认识Spring-2.1 自动装配(1)-byName
- 从头认识Spring-2.1 自动装配(3)-constructor
- Spring 框架--byType自动装配
- 从头认识Spring-2.2 默认自动装配
- Spring Autowire自动装配byName和byType
- Spring的自动装配(byName;byType)
- Spring的自动装配(byName;byType)
- Spring的自动装配(byName;byType)
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)
- 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(2)
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)
- 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)
- 从头认识Spring-2.3 注解装配-@autowired(2)-通过set方法或者其他方法注入
- 从头认识Spring-2.3 注解装配-@autowired(1)-通过属性域注入
- Spring自动装配之byName和byType【Spring入门】
- Spring自动装配模式三:byType的解析
- 36. Valid Sudoku LeetCode
- leetcode Climbing Stairs
- Android应用开发中保持手机屏幕常亮的方法
- 名词解释之人身保险费率
- jfinal学习笔记一(配置部署)
- 从头认识Spring-2.1 自动装配(2)-byType(1)
- 锻炼计划
- a标签被alert()显示href。
- JavaScript操作Cookie
- Java操作MongoDB
- Codeforces #341div2 B.Wet Shark and Bishops(暴力)
- poj1247对半分
- 杀死应用进程的几种方法
- 1024. 科学计数法 (20)