11、spring的bean基础(3)
在本文中,主要介绍的知识点有以下三个
- ListFactoryBean
- SetFactoryBean
- 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>