鍍金池/ 教程/ Java/ Spring ListFactoryBean實(shí)例
Spring EL hello world實(shí)例
Spring使用@Autowired注解自動裝配
MapFactoryBean實(shí)例
Bean InitializingBean和DisposableBean實(shí)例
Spring使用@Required注解依賴檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實(shí)例
Spring由類型(Type)自動裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實(shí)例
Struts 2 + Spring + Hibernate集成實(shí)例
Spring bean引用實(shí)例
Spring JdbcTemplate batchUpdate()實(shí)例
Spring SetFactoryBean實(shí)例
Spring通過setter方法注入
Spring EL hello world實(shí)例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實(shí)例
Spring自動裝配Beans
Spring由構(gòu)造方法自動裝配
Spring使用@Required注解依賴檢查
Spring SimpleJdbcTemplate查詢示例
自定義@Required-style注解
Spring+JDBC實(shí)例
Spring EL三元運(yùn)算(if-then-else)實(shí)例
JdbcTemplate+JdbcDaoSupport實(shí)例
Spring自動掃描組件
Spring EL方法調(diào)用實(shí)例
Spring自動裝配Beans
Spring EL bean引用實(shí)例
Spring依賴注入(DI)
Spring EL Lists,Maps實(shí)例
Spring通過構(gòu)造方法依賴注入
通過MailSender發(fā)送電子郵件
Spring AOP實(shí)例(Pointcut, Advisor)
Spring EL運(yùn)算符實(shí)例
Spring JavaConfig實(shí)例
Spring依賴注入 (DI)
Spring EL bean引用實(shí)例
Spring由AutoDetect自動裝配
Spring由構(gòu)造方法自動裝配
Spring JdbcTemplate查詢實(shí)例
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring+JDBC實(shí)例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring PropertyPlaceholderConfigurer實(shí)例
Spring JdbcTemplate batchUpdate() 實(shí)例
Spring EL與ExpressionParser測試
Spring內(nèi)部Bean實(shí)例
加載多個Spring bean配置文件
Spring+Hibernate+MySql實(shí)例(注解)
Spring AOP+AspectJ 在XML配置實(shí)例
Spring自動代理創(chuàng)建者實(shí)例
Spring EL正則表達(dá)式實(shí)例
測試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實(shí)例
Spring bean配置繼承
Spring使用Setter依賴注入
Spring自動裝配@Qualifier實(shí)例
Spring依賴檢查
Spring hello world實(shí)例
Spring依賴注入servlet會話監(jiān)聽器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實(shí)例
Spring教程
Spring自動裝配@Qualifier實(shí)例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實(shí)例
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring通過自動檢測自動裝配
Spring+Hibernate+MySql實(shí)例
Spring JavaConfig @Import實(shí)例
訪問MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢實(shí)例
Spring JavaConfig @Import實(shí)例
Spring Object/XML映射實(shí)例
Spring Bean引用例子
Spring AOP+AspectJ注解實(shí)例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴檢查
Spring JavaConfig實(shí)例
Spring AOP+AspectJ注解實(shí)例
Spring bean配置繼承
Spring自動掃描組件
自動代理創(chuàng)建者實(shí)例
Spring Bean init-method 和 destroy-method實(shí)例
Spring通過構(gòu)造方法注入
Spring過濾器組件自動掃描
Spring構(gòu)造方法注入類型歧義
Spring+JDBC實(shí)例
Spring EL正則表達(dá)式實(shí)例
Spring通過Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實(shí)例
SetFactoryBean實(shí)例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實(shí)例 – Advice
Spring使用@Autowired注解自動裝配
Spring AOP實(shí)例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門
Spring內(nèi)部bean實(shí)例
Spring bean加載多個配置文件
Spring + Hibernate+ MySql注解實(shí)例
Spring @PostConstruct和@PreDestroy實(shí)例
Spring依賴注入servlet會話監(jiān)聽器
Spring ListFactoryBean實(shí)例
Spring松耦合的實(shí)例
Spring過濾器組件自動掃描
Spring按名稱(Name)自動裝配
Spring AOP通知實(shí)例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實(shí)例
Spring EL操作符實(shí)例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實(shí)例
Struts2+Spring集成實(shí)例
Spring集合 (List,Set,Map,Properties) 實(shí)例
Spring按類型(Type)自動裝配
Spring由名稱(Name)自動裝配
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring hello world實(shí)例
Spring Bean InitializingBean和DisposableBean實(shí)例
Spring構(gòu)造方法注入類型歧義
Spring EL三元操作符(if-then-else)實(shí)例
Spring+Hibernate+MySql實(shí)例
Spring松耦合實(shí)例

Spring ListFactoryBean實(shí)例

ListFactoryBean”類為開發(fā)者提供了一種在Spring的bean配置文件中創(chuàng)建一個具體的列表集合類(ArrayList和LinkedList)。
這里有一個 ListFactoryBean 示例,在運(yùn)行時它將實(shí)例化一個ArrayList,并注入到一個 bean 屬性。
package com.yiibai.common;

import java.util.List;

public class Customer 
{
	private List lists;
	//...
}
Spring bean配置文件 - applicationContext.html 文件的內(nèi)容。
<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> 來達(dá)到同樣的目的。
<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 模式,否則會出現(xiàn)下面的錯誤
Caused by: org.xml.sax.SAXParseException: 
	The prefix "util" for element "util:list" is not bound.

執(zhí)行,查看結(jié)果:

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);
		
	}
}

輸出結(jié)果

Customer [lists=[one, 2, three]] Type=[class java.util.ArrayList]
在運(yùn)行時實(shí)例化ArrayList并注入列表到客戶的屬性。