1. 程式人生 > >Spring的《XML顯式裝配bean》之通過構造器注入Bean

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