11、spring的bean基础(3)

本文详细介绍如何使用Spring框架的ListFactoryBean、SetFactoryBean和MapFactoryBean进行集合类型的注入,并提供了具体的XML配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

11、spring的bean基础(3)

在本文中,主要介绍的知识点有以下三个

  1. ListFactoryBean
  2. SetFactoryBean
  3. MapFactoryBean

现项目中存在一个实体类,将用于以下三个例子的演示
HelloWorld.java

public class HelloWorld{
    private List list;
    private Set set;
    private Map map;

    //.....
}

ListFactoryBean示例
在spring bean配置文件中

<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-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="list">
            <bean class="org.springframework.beans.factory.config.ListFactoryBean">
                <property name="targetListClass">
                    <value>java.util.ArrayList</value>
                </property>
                <property name="sourceList">
                    <list>
                        <value>hello</value>
                        <value>1</value>
                        <value>three</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

</beans>

还可以使用下方式:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="CustomerBean" class="com.yiibai.common.Customer">
        <property name="list">
            <util:list list-class="java.util.ArrayList">
                <value>Hello</value>
                <value>1</value>
                <value>three</value>
            </util:list>
        </property>
    </bean>

</beans>

如果你不把util:list模式包含进配置文件中:或许会出现以下错误:

Caused by: org.xml.sax.SAXParseException: 
    The prefix "util" for element "util:list" is not bound.

SetFactoryBean实例

<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-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="set">
            <bean class="org.springframework.beans.factory.config.SetFactoryBean">
                <property name="targetSetClass">
                    <value>java.util.HashSet</value>
                </property>
                <property name="sourceSet">
                    <list>
                        <value>one</value>
                        <value>2</value>
                        <value>three</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>
</beans>

也可以使用以下模式:

<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-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="set">
            <util:set set-class="java.util.HashSet">
                <value>one</value>
                <value>2</value>
                <value>three</value>
            </util:set>
        </property>
    </bean>
</beans>

MapFactoryBean实例

<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-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="map">
            <bean class="org.springframework.beans.factory.config.MapFactoryBean">
                <property name="targetMapClass">
                    <value>java.util.HashMap</value>
                </property>
                <property name="sourceMap">
                    <map>
                        <entry key="Key1" value="one" />
                        <entry key="Key2" value="two" />
                        <entry key="Key3" value="three" />
                    </map>
                </property>
            </bean>
        </property>
    </bean>
</beans>

也可以使用以下方式:

<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-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="map">
            <util:map map-class="java.util.HashMap">
                <entry key="Key1" value="1" />
                <entry key="Key2" value="2" />
                <entry key="Key3" value="3" />
            </util:map>
        </property>
    </bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值