Spring的《XML显式装配bean》之通过构造器注入Bean

来源:互联网 发布:抓包软件下载 编辑:程序博客网 时间:2024/06/11 10:54

本文主要讲解两点:
1.怎么样声明一个bean
2.通过构造器注入bean

1. 怎么样声明一个bean?

1) 创建一个类:

package spring.ch1.topic5;public class Song {    private String name;    public Song(String name) {        this.name = name;    }    public Song() {    }    @Override    public String toString() {        return "the song:" + name;    }}

2)配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    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"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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="song"        class="spring.ch1.topic5.Song">        <constructor-arg value="there will be" />    </bean></beans>

这一句表示往构造器里面注入参数。

2.怎样通过构造器注入Bean?

下面以厨师制作蛋糕为例子。
(1)创建厨师类,在构建厨师的对象时,我们把需要制作的蛋糕的对象也传进去。

package spring.ch1.topic5;public class Chief {    private Cake cake = null;    public Chief(Cake cake) {//构造器注入的地方        this.cake = cake;    }    public void makeOneCake() {        System.out.println(cake.toString());    }}

(2)创建蛋糕类,通过final来标识每一个对象的id

package spring.ch1.topic5;public class Cake {    private final int id = index++;    private static int index = 0;    @Override    public String toString() {        return "create the cake,its id:" + id;    }}

(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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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="spring.ch1.topic5.Cake">    </bean>    <bean id="chief"        class="spring.ch1.topic5.Chief">        <constructor-arg ref="cake"/>    </bean></beans>

配置文件这里主要通过bean标签来配置:
在Bean标签里面还可以写入constructor-arg 标签,这里就是通过构造器注入,他里面有两个参数,一个是ref,引用上面某个bean;一个是value,直接写入值(比如string类型数据)。

(4)测试类

package spring.ch1.topic5;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 = {        "/spring/ch1/topic5/ApplicationContext-test.xml" })public class ChiefTest {    @Autowired    private ApplicationContext applicationContext;    @Test    public void testChief() {        Chief chief = applicationContext.getBean(Chief.class);        chief.makeOneCake();    }}

运行结果:

create the cake,its id:0
0 0
原创粉丝点击