Spring學習筆記--注入Bean屬性


這里通過一個MoonlightPoet類來演示了注入Bean屬性property的效果。

package com.moonlit.myspring;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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

import java.util.Properties;

public class MoonlightPoet {
    private String name;
    private int age;
    private Poem poem;
    private List<String> list;
    private Map<String, String> map;
    private Properties properties;
    public void perform() {
        System.out.println("name : " + name);
        System.out.println("age : " + age);
        poem.recite();
        for (String val : list) 
            System.out.println("in list : " + val);
        for (Entry<String, String> entry : map.entrySet())
            System.out.println("in map : " + entry.getKey() + " -- " + entry.getValue());
        for (Entry<Object, Object> entry : properties.entrySet())
            System.out.println("in properties : " + entry.getKey() + " -- " + entry.getValue());
    }
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring-idol.xml");
        MoonlightPoet moonlightPoet = (MoonlightPoet) context.getBean("moonlightPoet");
        moonlightPoet.perform();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Poem getPoem() {
        return poem;
    }
    public void setPoem(Poem poem) {
        this.poem = poem;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

該bean在xml文件中定義如下:

  <bean id="moonlightPoet" class="com.moonlit.myspring.MoonlightPoet">
    <property name="name" value="moonlit" />
    <property name="age" value="22" />
    <property name="poem" ref="sonnet29" />
    <property name="list">
      <list>
        <value>hello</value>
        <value>world</value>
        <!-- if bean, use <ref bean="XX"> -->
      </list>
    </property>
    <property name="map">
      <map>
        <entry key="key1" value="value1" />
        <entry key="key2" value="value2" />
        <entry key="key3" value="value3" />
      </map>
    </property>
    <property name="properties">
      <props>
        <prop key="GUITAR">STRUM STRUM STRUM</prop>
        <prop key="CYMBAL">CRASH CRASH CRASH</prop>
          <prop key="HARMONICA">HUM HUM HUM</prop>
      </props>
    </property>
  </bean>

輸出結果:

name : moonlit
age : 22
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself, and curse my fate,
Wishing me like to one more rich in hope,
Featur'd like him, like him with friends possess'd,
Desiring this man's art and that man's scope,
With what I most enjoy contented least;
Yet in these thoughts myself almost despising,
Haply I think on thee, and then my state,
Like to the lark at break of day arising
From sullen earth, sings hymns at heaven's gate;
For thy sweet love remember'd such wealth brings
That then I scorn to change my state with kings.
in list : hello
in list : world
in map : key1 -- value1
in map : key2 -- value2
in map : key3 -- value3
in properties : HARMONICA -- HUM HUM HUM
in properties : CYMBAL -- CRASH CRASH CRASH
in properties : GUITAR -- STRUM STRUM STRUM

理解:
注入簡單值:
<property name="XX" value="YY" />
其中XX是變量名,YY是值。
引用其他Bean:
<property name="XX" ref="YY">
其中XX是變量名,YY是引用的bean的id。
也可以注入內部類:
<property name="XX">
  <bean class="YY" />
</preperty>
其中XX是變量名,YY是內部類對應的類名。
裝配List、Set和Array:
<property name="XX">
  <value>YY</value>
  或者
  <ref bean="ZZ">
</property>
其中XX是變量名,YY是值,ZZ是引用的bean。
裝配Map:
<map>
  <entry key="XX" value="YY" />
  或者
  <entry key="XX" value-ref="YY" />
  或者
  <entry key-ref="XX" value="YY" />
  或者
  <entry key-ref="XX" value-ref="YY" />
</map>
因為map的key和value可以對應一個基礎類型的值,也可以對應一個bean,所以key,value對應值,key-ref,value-ref對應bean。
裝配Properties集合:
<property name="XX">
  <props>
    <prop key="AA">BB</prop>
    ....
  </props>
</property>
其中AA對應key,BB對應value。
裝配空值:使用<null/>元素。

使用Spring的命名空間p裝配屬性
我們可以在beans中添加

xmlns:p="http:www.springframework.org/schema/beans"

來使用p:作為<bean>元素所有屬性的前綴來裝配Bean的屬性。用法如下:

<bean id="kenny" class="XX"
    p:song = "Jingle Bells"
    p:instrument-ref = "saxphone" />

-ref后綴作為一個標識來告知Spring應該裝配一個引用而不是字面值。


免責聲明!

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



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