从头认识Spring-2.1 自动装配-byName

Posted 李灵晖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识Spring-2.1 自动装配-byName相关的知识,希望对你有一定的参考价值。

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

我们还是以例子来说明:

1.domain

蛋糕类:(为了方便,只保留name属性以示区别)

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;
	}

}


这里有一个注意点,就是Cake对象在注入的时候,会根据属性的名称来搜查,因此,这里的名字需要跟xml里面蛋糕Bean的id一样,这样才能在后面的测试里面顺利通过

 

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="cake"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_1.Cake"
		p:name="blueberryCheeseCake" scope="prototype" />

	<bean id="jack"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_1.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_1.Chief"
		autowire="byName" p:name="rose" />
</beans>


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

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

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

 

测试输出:

jack make blueberryCheeseCake
rose make blueberryCheeseCake

 

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

 

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

 

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

 

以上是关于从头认识Spring-2.1 自动装配-byName的主要内容,如果未能解决你的问题,请参考以下文章

从头认识Spring-2.1 自动装配-byType

从头认识Spring-2.1 自己主动装配-byType

从头认识Spring-2.2 默认自动装配

spring中自动装配byName,byType,constructor

自动装配 Bean

spring自动装配