从头认识Spring-2.1 自动装配(2)-byType(1)
2016-02-11 23:39
429 查看
这一章节我们来讨论一下自动装配的第二种方式-byType
1.domain
蛋糕类:(不变)
厨师类:(不变)
2.测试类:(不变)
3.配置文件(重点)
在上面的配置文件里面我们需要注意的地方:
在rose里面,没有了cake属性的注入,但是增加了autowire="byType"这一句。
autowire="byType":表示当Bean的类型跟注入属性的类型相一致的时候,即可注入,也就是,当rose里面有一个属性需要注入Cake类型,在配置文件里面只需要有一个bean的类型是Cake,那么,系统就会自动的把bean注入到属性里面去
但是这里有一个问题,当配置文件里面有多个Cake,这时候系统会抛异常,因为不知道注入那个对象
下面是错误的配置文件:
测试输出:
jack make blueberryCheeseCake
rose make blueberryCheeseCake
总结:这一章节主要介绍了autowire属性里面的byType选项的使用。
目录:/article/3641905.html 我的github:https://github.com/raylee2015/my_new_spring
1.domain
蛋糕类:(不变)
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_1; 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_1; 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_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_1/ApplicationContext-test.xml" }) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public 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.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_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.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_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选项的使用。
目录:/article/3641905.html 我的github:https://github.com/raylee2015/my_new_spring
相关文章推荐
- Struts2利用拦截器实现权限控制
- 从头认识Spring-2.1 自动装配(1)-byName
- java基础12多线程
- java中的构造方法
- java中的this
- java常见问题
- Java—设计模式(工厂模式)
- 栈的应用--四则运算表达式求值(java代码)
- JAVA比较两个日期相差的天数
- Arcgis server java develop begining
- Gradle教程Part2:java工程
- Java开发环境搭建
- java基础总结 --- enum枚举实现多路分发 石头、剪刀、布游戏示例
- The Java™ Tutorials — Concurrency :Memory Consistency Errors 内存一致性错误
- Spark学习笔记7-在eclipse里用scala编写spark程序(单机和集群运行)
- JDK中自带xml和对象互转方法
- 蓝桥杯 算法训练 最小乘积(基本型)
- JAVA常见基本术语
- java synchronized详解(转载)
- Java多线程ExecutorService.shutdown什么时候执行