Spring學習筆記 關於Bean屬性的初始化 - 使用inner bean以及List, Map與Set的初始化

對於引用類型的屬性,可以使用<property name="屬性名" ref="×××" />進行初始化。如下圖,ref="messager",messager爲另外定義的一個id爲messager的bean。

 
<beans />標籤內定義的帶有id或者name屬性的<bean />標籤可以在Client代碼中引用到進行使用。如果上圖中定義的id 爲messager的bean(第二個紅框)的目的只是初始化messageLooper中的屬性<property name="messager"  />,而不想被Client代碼訪問,這時就可以使用inner bean去初始化<property name="messager"  />。如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="messageLooper" class="demo.MessageLooper">
	    <property name="messager" >
		<!-- 以下bean的定義用來初始化property messager,注意以下定義的bean沒有id和name屬性,爲inner bean-->
	        <bean class="demo.GoodbyeMessagerImpl" />
	    </property>
	    <property name="numTimes" value="3" />
	</bean>
	
</beans>

 

List類型屬性的初始化:

<bean id="collectionTester" class="property.ListTester">
    <property name="demoList" >   
        <list>
            <value>entry1</value>
            <value>entry2</value>
            <value>entry3</value>
        </list>
    </property>
</bean>


也可以使用ref標籤初始化列表內存放引用變量的的List

<bean id="collectionTester" class="property.ListTester">
    <property name="demoList" >   
        <list>
            <ref bean="類名"/>
        </list>
    </property>
</bean>

示例:

 
Set類型屬性初始化:
<property name="demoList" >   
      <set>
          <value>entry1</value>
          <value>entry2</value>
          <value>entry3</value>
      </set>
</property>

 

 

Map類型屬性初始化:
<property name=urlParamsMappingMap">
    <map>
        <entry key="businessOne">
	    <value>ACCOUNT=userid | TICKET=authid </value>
        </entry>
        <entry key="businessTwo">
            <value>ACCOUNT=person_id | TICKET=pid </value>
        </entry>
    </map>
</property>

 

示例代碼:

main運行類

package property;

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

public class CollectionTesterMain
{

    public static void main(String[] args)
    {
        String springConfig = "property/spring-config.xml";
        ApplicationContext spring = 
                new ClassPathXmlApplicationContext(springConfig);
        CollectionTester collectionTester =
                spring.getBean("collectionTester", CollectionTester.class);
        collectionTester.printCollection();
    }

}


接口類

package property;

public interface CollectionTester
{
    public void printCollection();
}


接口實現類

package property;

import java.util.List;

public class ListTester implements CollectionTester
{
    private List<String> demoList;
    public List<String> getDemoList()
    {
        return demoList;
    }
    public void setDemoList(List<String> demoList)
    {
        this.demoList = demoList;
    }

    @Override
    public void printCollection()
    {
        if (demoList == null || demoList.size() == 0)
        {
            System.out.println("列表爲空。");
        }
        else
        {
            for (String listEntry : demoList)
            {
                System.out.println(listEntry);
            }
        }
    }
}


Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="collectionTester" class="property.ListTester">
		<property name="demoList" >   
           <list>
               <value>entry1</value>
               <value>entry2</value>
               <value>entry3</value>
           </list>
		</property>
	</bean>
</beans>


 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章