【Spring實戰】—— 5 設值注入


本篇主要講解了Spring的最常用的功能——依賴注入。

注入的方式,是使用Getter Setter注入,平時大多的編程也都是使用這種方法。

  舉個簡單的例子,還是表演者。

  表演者有自己的屬性,年齡或者表演的歌曲等等。還需要一些復雜的屬性,比如樂器,每一種樂器會發出不同的聲音。

  下面看一下表演者Performer

package com.spring.test.action1;

public interface Performer {
    void perform() throws PerformanceException;
}

  我們自己定義一個鋼琴演奏者,該表演者有年齡和歌曲,還有額外的一種樂器屬性。

package com.spring.test.setter;

import com.spring.test.action1.PerformanceException;
import com.spring.test.action1.Performer;

public class Instrumentalist implements Performer{
    private String song;
    private int age;
    private Instrument instrument;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSong() {
        return song;
    }
    public void setSong(String song) {
        this.song = song;
    }
    public Instrument getInstrument() {
        return instrument;
    }
    public void setInstrument(Instrument instrument) {
        this.instrument = instrument;
    }
    public Instrumentalist(){}
    public void perform() throws PerformanceException {
        System.out.println("Instrumentalist age:"+age);
        System.out.print("Playing "+song+":");
        instrument.play();
    }
}

  樂器的構造如下,依然使用接口方式:

package com.spring.test.setter;

public interface Instrument {
    public void play();
}

  薩克斯實現該樂器接口

package com.spring.test.setter;

public class Saxophone implements Instrument {
    public Saxophone(){}
    public void play() {
        System.out.println("TOOT TOOT TOOT");
    }
}

  以上就是基本的類的構造了。

  下面看一下Spring的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="sax" class="com.spring.test.setter.Saxophone"/>
     <bean id="kenny" class="com.spring.test.setter.Instrumentalist">
         <property name="song" value="Jingle Bells" />
         <property name="age" value="25" />
         <property name="instrument" ref="sax" />
     </bean>
</beans>

  在配置文件中,可以發現,設值注入時,使用name來指定注入哪個屬性

  name的命名方式依據變量名稱。

  1 首字母不區分大小寫,其他部分與變量名稱相同。

  2 注入的屬性類型,可以是String , int , double , float等,當屬性是String或int時,可以根據變量的類型自動轉換。

  3 注入的是一個bean,則直接使用ref鏈接到另一個bean即可。

  下面是應用上下文的代碼:

package com.spring.test.setter;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test.action1.PerformanceException;

public class test {
    public static void main(String[] args) throws PerformanceException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
        performer.perform();
    }
}

  執行結果如下:

Instrumentalist age:25
Playing Jingle Bells:TOOT TOOT TOOT

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM