• Spring ListFactoryBean实例


    ListFactoryBean”类为开发者提供了一种在Spring的bean配置文件中创建一个具体的列表集合类(ArrayList和LinkedList)。
    这里有一个 ListFactoryBean 示例,在运行时它将实例化一个ArrayList,并注入到一个 bean 属性。
    package com.yiibai.common;
    
    import java.util.List;
    
    public class Customer 
    {
    	private List lists;
    	//...
    }
    Spring bean配置文件 - applicationContext.html 文件的内容。
    <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.yiibai.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>one</value>
    						<value>2</value>
    						<value>three</value>
    					</list>
    				</property>
    			</bean>
    		</property>
    	</bean>
    
    </beans>
    另外,还可以使用 util 模式和<util:list> 来达到同样的目的。
    <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="lists">
    			<util:list list-class="java.util.ArrayList">
    				<value>one</value>
    				<value>2</value>
    				<value>three</value>
    			</util:list>
    		</property>
    	</bean>
    
    </beans>
    请记住要包函 util 模式,否则会出现下面的错误
    Caused by: org.xml.sax.SAXParseException: 
    	The prefix "util" for element "util:list" is not bound.

    执行,查看结果:

    package com.yiibai.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(
    				"applicationContext.xml");
    
    		Customer cust = (Customer) context.getBean("CustomerBean");
    		System.out.println(cust);
    		
    	}
    }

    输出结果

    Customer [lists=[one, 2, three]] Type=[class java.util.ArrayList]
    在运行时实例化ArrayList并注入列表到客户的属性。
  • 相关阅读:
    SimpleXML读写XML文件以及json的读写
    XMLWriter和XMLReader读写XML文件
    保护眼睛——设置WIN7和XP 窗体、PDF、网页背景颜色(IE、Chrome、Firefox)
    DevExpress Winform通用控件开发总结
    SQL Server远程连接设置
    JSON-C库的使用(2)Json对象的遍历
    JSON-C库的使用(1)Json对象数组的解析
    SQL Server数据类型,System.Data.SqlDbType,.NET数据类型
    4.2 access函数实例
    4.1 对每个命令行参数打印文件类型
  • 原文地址:https://www.cnblogs.com/soundcode/p/6367279.html
Copyright © 2020-2023  润新知