您的位置:首页 > 编程语言 > Java开发

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

2016-02-11 23:39 429 查看
这一章节我们来讨论一下自动装配的第二种方式-byType
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: