现在的位置: 首页 > 综合 > 正文

Spring学习笔记 关于Bean属性的初始化 – 使用inner bean以及List, Map与Set的初始化

2013年06月11日 ⁄ 综合 ⁄ 共 2942字 ⁄ 字号 评论关闭

对于引用类型的属性,可以使用<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>

 

抱歉!评论已关闭.