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

Spring的《XML显式装配bean》- XML自动装配

2017-01-22 19:43 323 查看
这一篇blog主要介绍通过xml配置自动装配,主要包括以下内容:

1. 自动装配的第一种方式-byName

2. 自动装配的第二种方式-byType

3. 自动装配的第三种方式-constructor

对于每种方式的介绍均以例子来说明:

1.自动装配的第一种方式-byName

(1)entity

蛋糕类:这里只保留了name属性。

package spring.ch2.topic1;

/**
* Created by louyuting on 17/1/20.
* 注入属性,记得属性必须要写setter方法  不然就会抛出异常,注入失败.
* 蛋糕类
*/
public class Cake {
private String name = "";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


厨师类:厨师类里面只保留了name属性和厨师所做的蛋糕。

package spring.ch2.topic1;

/**
* Created by louyuting on 17/1/20.
* 厨师类
*/
public class Chief {
private String name = "";

private Cake cake = null;

public Cake getCake() {
return cake;
}
public String getName() {
return name;
}
public void setCake(Cake cake) {
this.cake = cake;
}
public void setName(String name) {
this.name = name;
}

public Cake makeOneCake() {
System.out.println(getName() + " make " + getCake().getName());
return cake;
}
}


Chief类中的cake对象是通过spring的byName自动注入的,byName自动注入的机制会根据属性的名称来搜查,因此,这里的名字需要跟xml里面蛋糕Bean的id一样,这样才能在后面的测试里面顺利通过。

(2)配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="cake"
class="spring.ch2.topic1.Cake"
p:name="blueberryCheese---Cake" scope="prototype">
</bean>
<bean id="jack"
class="spring.ch2.topic1.Chief"
p:name="jack">
<property name="cake" ref="cake" />
</bean>
<bean id="rose"
class="spring.ch2.topic1.Chief"
autowire="byName" p:name="rose">
</bean>
</beans>


配置文件里面注入了一个Cake的蛋糕类,id是cake,和Chief中的Cake属性名称一样。对于jack的bean,通过property标签注入cake。但是对于rose,没有显式的注入,而是通过加了autowire=”byName”,表示通过name自动注入,byName,就是Cake对象在注入的时候,会根据属性的名称来搜查,因此,Cake类里面属性的名字需要跟xml里面蛋糕bean的id一样,这样才能在后面的测试里面顺利通过。

(3)测试类:

package spring.ch2.topic1;

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;

/**
* Created by louyuting on 17/1/20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/ch2/topic1/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();
}
}


运行结果:

jack make blueberryCheese---Cake
rose make blueberryCheese---Cake


表示jack和rose都注入成功了。

2.自动装配的第二种方式-byType

测试实例中,entity和测试类均不变,只改变配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="cake"
class="spring.ch2.topic1.Cake"
p:name="blueberryCheese---Cake" scope="prototype">
</bean>
<bean id="jack"
class="spring.ch2.topic1.Chief"
p:name="jack">
<property name="cake" ref="cake" />
</bean>
<bean id="rose"
class="spring.ch2.topic1.Chief"
autowire="byType" p:name="rose">
</bean>
</beans>


上面只改了一个地方,把rose这个bean的autowire改为了:autowire=”byType”。表示通过类型自动注入:表示当Bean的类型跟注入属性的类型相一致的时候 ,也就是,当rose里面有一个属性需要注入Cake类型,在配置文件里面只需要有一个bean的类型是Cake,那么系统就会自动的把bean注入到属性里面去。但是有多个bean的类型是Cake,就会抛出异常。

运行结果:

jack make blueberryCheese---Cake
rose make blueberryCheese---Cake


说明通过byType自动注入成功了。

注意:但是有多个bean的类型是Cake,就会抛出异常,因为不知道注入哪个Cake。比如下面的错误的配置:

<bean id="cake"
class="spring.ch2.topic1.Cake"
p:name="blueberryCheese---Cake" scope="prototype">
</bean>

<bean id="cake2"
class="spring.ch2.topic1.Cake"
p:name="hehehehe" scope="prototype">
</bean>

<bean id="jack"
class="spring.ch2.topic1.Chief"
p:name="jack">
<property name="cake" ref="cake" />
</bean>
<bean id="rose"
class="spring.ch2.topic1.Chief"
autowire="byType" p:name="rose">
</bean>


这个时候,里面有两个Cake的bean,通过byType注入的时候就不知道注入哪个Cake,就会抛出异常UnsatisfiedDependencyException。

那么有没有办法解决配置文件里面出现多个同类型的Bean而byType无法匹配的问题呢?当然是有的,通过引入了autowire-candidate属性来解决这个问题:

先来说说autowire-candidate这个属性的含义:

这个属性的意思是,是否排除自己,如果排除了自己, 那么当使用byType的时候,这个bean就不列入候选bean里面。比如当autowire-candidate = false 时就表示当前bean不在候选的bean里面。

下面我们修改一下上面的错误的配置文件,使其能够工作:

<bean id="cake"
class="spring.ch2.topic1.Cake"
p:name="blueberryCheese---Cake" scope="prototype">
</bean>

<bean id="cake2"
class="spring.ch2.topic1.Cake"
p:name="hehehehe" scope="prototype" autowire-candidate="false">
</bean>

<bean id="jack"
class="spring.ch2.topic1.Chief"
p:name="jack">
<property name="cake" ref="cake" />
</bean>
<bean id="rose"
class="spring.ch2.topic1.Chief"
autowire="byType" p:name="rose">
</bean>


我再cake2的bean中添加了autowire-candidate=”false”,表示当前bean不在候选的bean中,这是再运行测试类,就能运行成功了。

3. 自动装配的第三种方式-constructor

(1)entity

蛋糕类:(不变)

package spring.ch2.topic1;

/**
* Created by louyuting on 17/1/20.
* 注入属性,记得属性必须要写setter方法  不然就会抛出异常,注入失败.
* 蛋糕类
*/
public class Cake {
private String name = "";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


厨师类(增加了构造器)

package spring.ch2.topic1;

/**
* Created by louyuting on 17/1/20.
* 厨师类
*/
public class Chief {
private String name = "";

private Cake cake = null;

//构造器
public Chief(Cake cake) {
this.cake = cake;
}

public Cake getCake() {
return cake;
}
public String getName() {
return name;
}
public void setCake(Cake cake) {
this.cake = cake;
}
public void setName(String name) {
this.name = name;
}

public Cake makeOneCake() {
System.out.println(getName() + " make " + getCake().getName());
return cake;
}
}


(2)配置文件:

<bean id="cake"
class="spring.ch2.topic1.Cake"
p:name="blueberryCheese---Cake" scope="prototype">
</bean>
<bean id="jack"
class="spring.ch2.topic1.Chief"
p:name="jack" autowire="constructor">
</bean>


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

在jack里面,没有了cake属性的注入,但是增加了autowire=”constructor”这一句。autowire=”constructor”:表示当bean的类型跟构造器注入的类型相一致、或者Bean的id跟构造器注入的名称相一致的时候,即可注入,也就是,当jack的构造器需要注入Cake类型,而在配置文件里面有一个bean的类型是Cake,那么,系统就会自动的把bean注入到属性里面去,或者当jack的构造器需要注入的是cake名称的对象,而在配置文件里面有那么一个bean,系统就可以自动注入。

但是这里有一个问题,当配置文件里面有多个Cake,这时候系统会抛异常:

(1)当有两个Cake,但是其中一个bean的id跟注入的名字相同

(2)当没有相同名称的时候,但是又存在两个Cake的Bean,就会抛异常

(3)测试类(不变)

完整代码的github地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring