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

本文主要讲解两点:
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值