Spring ListFactoryBean example

本文介绍如何利用Spring框架中的ListFactoryBean来创建并注入ArrayList实例。通过配置文件示例,展示了两种方法:直接使用ListFactoryBean和利用util schema简化配置。运行结果验证了配置的有效性。

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

The ‘ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.

Here’s a ListFactoryBean example, it will instantiate an ArrayList at runtime, and inject it into a bean property.

package com.mkyong.common;

import java.util.List;

public class Customer 
{
    private List lists;
    //...
}

Spring’s bean configuration file.

<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="CustomerBean" class="com.mkyong.common.Customer">
        <property name="lists">
            <bean class="org.springframework.beans.factory.config.ListFactoryBean">
                <property name="targetListClass">
                    <value>java.util.ArrayList</value>
                </property>
                <property name="sourceList">
                    <list>
                        <value>1</value>
                        <value>2</value>
                        <value>3</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

</beans>

Alternatively, you also can use util schema and <util:list> to achieve the same thing.

<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.mkyong.common.Customer">
        <property name="lists">
            <util:list list-class="java.util.ArrayList">
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </util:list>
        </property>
    </bean>

</beans>

Remember to include the util schema, else you will hit the following error

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

Run it…

package com.mkyong.common;

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

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "SpringBeans.xml");

        Customer cust = (Customer) context.getBean("CustomerBean");
        System.out.println(cust);

    }
}

Ouput

Customer [lists=[1, 2, 3]] Type=[class java.util.ArrayList]

You have instantiated ArrayList and injected it into Customer’s lists property at runtime.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值