Spring 的四種方式依賴注入

一、Spring 基於構造函數的依賴注入

package com.home.model;
public class Teditor {
    private Specker specker;
    public Teditor(Specker specker){
        System.out.println("Inside Teditor constructor.");
        this.specker=specker;
    }
    public void speckeCheck(){
        specker.Speckering();
    }
}
public class Specker {
    public Specker(){
        System.out.println("Inside Specker constructor.");
    }
    public void Speckering(){
        System.out.println("Inside Speckering!");
    }

}
//以下爲配置文件
<!-- 基於構造函數的依賴注入 -->
    <bean id="teditor" class="com.home.model.Teditor">
       <constructor-arg ref="specker"></constructor-arg>
    </bean>
    <bean id="specker" class="com.home.model.Specker"></bean>

//測試代碼
 @RequestMapping(value="/teditor",method=RequestMethod.GET)
    public ModelAndView DITEST(){
        ModelAndView view=new ModelAndView("/login/hello");
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Teditor teditor=(Teditor) context.getBean("teditor");
        teditor.speckeCheck();
        return view;
    }

輸出結果:
Inside Specker constructor.
Inside Teditor constructor.
inside into Sparker construstor

二、Spring 基於設值函數的依賴注入

//SparkSystem類
public class SparkSystem {
    private Sparker sparker;
    public void setSparker(Sparker sparker){
        System.out.println("SparkSystem===Inside setSpark");
        this.sparker=sparker;
    }
    public Sparker getSparker(){
        System.out.println("SparkSystem==Inside get getPark");
        return sparker;
    }
    public void spark(){
        sparker.sparkering();
    }

}
//Sparker類
package com.home.model;
public class Sparker {
    public Sparker(){
        System.out.println("inside into Sparker construstor");
    }
    public void sparkering(){
        System.out.println("inside in Sparker for sparkering");
    }
}
//配置文件
 <!-- Spring 基於設值函數的依賴注入 -->
 <bean id="sparkSystem" class="com.home.model.SparkSystem">
      <property name="sparker" ref="sparker"></property>
 </bean>
 <bean id="sparker" class="com.home.model.Sparker"></bean>

三、Spring 注入內部 Beans
正如你所知道的 Java 內部類是在其他類的範圍內被定義的,同理,inner beans 是在其他 bean 的範圍內定義的 bean。因此在 或 元素內 元素被稱爲內部bean

package com.home.model;
//Computer類
public class Computer {
    private Message message;
    public Computer(){
        System.out.println("inside into Computer constructor");
    }
    public void setMessage(Message message){
        System.out.println("inside into setComputer");
        this.message=message;
    }
    public Message getMessage(){
        System.out.println("inside into getComputer");
        return message;
    }
    public void messageing(){
        message.messageing();
    }
}
//Message類
package com.home.model;
public class Message {
    public Message(){
        System.out.println("inside into Message constructor!");
    }
    public void messageing(){
        System.out.println("inside messageing....");
    }
}
//配置文件
<!-- 注入內部 Beans -->
    <bean id="computer" class="com.home.model.Computer">
        <property name="message">
            <bean id="message" class="com.home.model.Message">
            </bean>
        </property>
   </bean>

四、Spring 注入集合

  ****<list>它有助於連線,如注入一列值,允許重複。
  <set>它有助於連線一組值,但不能重複。 
  <map>它可以用來注入名稱-值對的集合,其中名稱和值可以是任何類型。    
  <props>它可以用來注入名稱-值對的集合,其中名稱和值都是字符串類型。****
package com.home.model;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionExample {
    List addressList;//它有助於連線,如注入一列值,允許重複
    Set  addressSet;//它有助於連線一組值,但不能重複
    Map  addressMap;//它可以用來注入名稱-值對的集合,其中名稱和值可以是任何類型
    Properties addressProp;//它可以用來注入名稱-值對的集合,其中名稱和值都是字符串類型
    public List getAddressList() {
        System.out.println("List:"+addressList);
        return addressList;
    }
    public void setAddressList(List addressList) {
        this.addressList = addressList;
    }
    public Set getAddressSet() {
        System.out.println("Set:"+addressSet);
        return addressSet;
    }
    public void setAddressSet(Set addressSet) {
        this.addressSet = addressSet;
    }
    public Map getAddressMap() {
        System.out.println("Map:"+addressMap);
        return addressMap;
    }
    public void setAddressMap(Map addressMap) {
        this.addressMap = addressMap;
    }
    public Properties getAddressProp() {
        return addressProp;
    }
    public void setAddressProp(Properties addressProp) {
        System.out.println("Prop:"+addressProp);
        this.addressProp = addressProp;
    }
}
//配置文件如下:
 <!-- 注入集合 -->
    <bean id="collectionExample" class="com.home.model.CollectionExample">
        <property name="addressList">
            <list>
               <value>China</value>
               <value>India</value>
               <value>India</value>
               <value>USA</value>
            </list>
        </property>
        <property name="addressSet">
            <set>
               <value>China</value>
               <value>India</value>
               <value>India</value>
               <value>USA</value>
            </set>
        </property>
        <property name="addressMap">
            <map>
                <entry key="1" value="China"></entry>
                <entry key="2" value="India"></entry>
                <entry key="3" value="India"></entry>
                <entry key="4" value="USA"></entry>
            </map>
        </property>
        <property name="addressProp">
             <props>
                 <prop key="one">China</prop> 
                 <prop key="two">India</prop> 
                 <prop key="three">India</prop> 
                 <prop key="four">USA</prop> 
             </props>
        </property>
    </bean>
//測試類
  @RequestMapping(value="/collection",method=RequestMethod.GET)
    public ModelAndView collection(){
        ModelAndView view=new ModelAndView("/login/hello");
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        CollectionExample collectionExample=(CollectionExample)context.getBean("collectionExample");
        collectionExample.getAddressList();
        collectionExample.getAddressSet();
        collectionExample.getAddressMap();
        collectionExample.getAddressProp();
        return view;
    }
打印結果如下:
List:[China, India, India, USA]
Set:[China, India, USA]
Map:{1=China, 2=India, 3=India, 4=USA}
Prop:{two=India, one=China, three=India, four=USA}
發佈了91 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章