spring xml 文件的一些写法,包括map ,set ,list 等
2017-06-26 16:39
603 查看
To switch over from the DTD-style to the new XML Schema-style, you need to make the following change.
The equivalent file in the XML Schema
To use the tags in the util schema, you need to have the following preamble at the top of your Spring XML configuration file; the bold text in the snippet below references the correct schema so that the tags in the util namespace are available to you.
Before…
The above configuration uses a Spring FactoryBean implementation, the FieldRetrievingFactoryBean, to set the value of the ‘isolation’ property on a bean to the value of the ‘java.sql.Connection.TRANSACTION_SERIALIZABLE’ constant. This is all well and good, but it is a tad verbose and (unneccessarily) exposes Spring’s internal plumbing to the end user.
The following XML Schema-based version is more concise and clearly expresses the developer’s intent (‘inject this constant value’), and it just reads better.
Setting a bean property or constructor arg from a field value
FieldRetrievingFactoryBean is a FactoryBean which retrieves a static or non-static field value. It is typically used for retrieving public static final constants, which may then be used to set a property value or constructor arg for another bean.
Find below an example which shows how a static field is exposed, by using the staticField property:
There is also a convenience usage form where the static field is specified as the bean name:
It is also possible to access a non-static (instance) field of another bean, as described in the API documentation for the FieldRetrievingFactoryBean class.
Injecting enum values into beans as either property or constructor arguments is very easy to do in Spring, in that you don’t actually have to do anything or know anything about the Spring internals (or even about classes such as the FieldRetrievingFactoryBean). Let’s look at an example to see how easy injecting an enum value is; consider this JDK 5 enum:
Now consider a setter of type PersistenceContextType:
.. and the corresponding bean definition:
This works for classic type-safe emulated enums (on
4000
JDK 1.4 and JDK 1.3) as well; Spring will automatically attempt to match the string property value to a constant on the enum class.
Before…
The above configuration uses a Spring FactoryBean implementation, the PropertyPathFactoryBean, to create a bean (of type int) called ‘testBean.age’ that has a value equal to the ‘age’ property of the ‘testBean’ bean.
After…
Before…
The above configuration uses a Spring FactoryBean implementation, the ListFactoryBean, to create a java.util.List instance initialized with values taken from the supplied ‘sourceList’.
After…
You can also explicitly control the exact type of List that will be instantiated and populated via the use of the ‘list-class’ attribute on the element. For example, if we really need a java.util.LinkedList to be instantiated, we could use the following configuration:
If no ‘list-class’ attribute is supplied, a List implementation will be chosen by the container.
Before…
The above configuration uses a Spring FactoryBean implementation, the MapFactoryBean, to create a java.util.Map instance initialized with key-value pairs taken from the supplied ‘sourceMap’.
After…
You can also explicitly control the exact type of Map that will be instantiated and populated via the use of the ‘map-class’ attribute on the element. For example, if we really need a java.util.TreeMap to be instantiated, we could use the following configuration:
If no ‘map-class’ attribute is supplied, a Map implementation will be chosen by the container.
Before…
The above configuration uses a Spring FactoryBean implementation, the SetFactoryBean, to create a java.util.Set instance initialized with values taken from the supplied ‘sourceSet’.
After…
You can also explicitly control the exact type of Set that will be instantiated and populated via the use of the ‘set-class’ attribute on the element. For example, if we really need a java.util.TreeSet to be instantiated, we could use the following configuration:
If no ‘set-class’ attribute is supplied, a Set implementation will be chosen by the container.
C.2.3 The jee schema
The jee tags deal with Java EE (Java Enterprise Edition)-related configuration issues, such as looking up a JNDI object and defining EJB references.
To use the tags in the jee schema, you need to have the following preamble at the top of your Spring XML configuration file; the bold text in the following snippet references the correct schema so that the tags in the jee namespace are available to you.
C.2.3.1 (simple)
Before…
C.2.3.2 (with single JNDI environment setting)
Before…
After…
(with multiple JNDI environment settings)
Before…
After…
参考此链接
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- <bean/> definitions here --> </beans>
The equivalent file in the XML Schema
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <!-- <bean/> definitions here --> </beans>
To use the tags in the util schema, you need to have the following preamble at the top of your Spring XML configuration file; the bold text in the snippet below references the correct schema so that the tags in the util namespace are available to you.
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- <bean/> definitions here --> </beans>
Before…
<bean id="..." class="..."> <property name="isolation"> <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" /> </property> </bean>
The above configuration uses a Spring FactoryBean implementation, the FieldRetrievingFactoryBean, to set the value of the ‘isolation’ property on a bean to the value of the ‘java.sql.Connection.TRANSACTION_SERIALIZABLE’ constant. This is all well and good, but it is a tad verbose and (unneccessarily) exposes Spring’s internal plumbing to the end user.
The following XML Schema-based version is more concise and clearly expresses the developer’s intent (‘inject this constant value’), and it just reads better.
<bean id="..." class="..."> <property name="isolation"> <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> </property> </bean>
Setting a bean property or constructor arg from a field value
FieldRetrievingFactoryBean is a FactoryBean which retrieves a static or non-static field value. It is typically used for retrieving public static final constants, which may then be used to set a property value or constructor arg for another bean.
Find below an example which shows how a static field is exposed, by using the staticField property:
<bean id="myField" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> </bean>
There is also a convenience usage form where the static field is specified as the bean name:
<bean id="..." class="..."> <property name="isolation"> <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" /> </property> </bean>
It is also possible to access a non-static (instance) field of another bean, as described in the API documentation for the FieldRetrievingFactoryBean class.
Injecting enum values into beans as either property or constructor arguments is very easy to do in Spring, in that you don’t actually have to do anything or know anything about the Spring internals (or even about classes such as the FieldRetrievingFactoryBean). Let’s look at an example to see how easy injecting an enum value is; consider this JDK 5 enum:
package javax.persistence; public enum PersistenceContextType { TRANSACTION, EXTENDED }
Now consider a setter of type PersistenceContextType:
package example; public class Client { private PersistenceContextType persistenceContextType; public void setPersistenceContextType(PersistenceContextType type) { this.persistenceContextType = type; } }
.. and the corresponding bean definition:
<bean class="example.Client"> <property name="persistenceContextType" value="TRANSACTION" /> </bean>
This works for classic type-safe emulated enums (on
4000
JDK 1.4 and JDK 1.3) as well; Spring will automatically attempt to match the string property value to a constant on the enum class.
Before…
<!-- target bean to be referenced by name --> <bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype"> <property name="age" value="10"/> <property name="spouse"> <bean class="org.springframework.beans.TestBean"> <property name="age" value="11"/> </bean> </property> </bean>
<!-- will result in 10, which is the value of property 'age' of bean 'testBean' --> <bean id="testBean.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
The above configuration uses a Spring FactoryBean implementation, the PropertyPathFactoryBean, to create a bean (of type int) called ‘testBean.age’ that has a value equal to the ‘age’ property of the ‘testBean’ bean.
After…
<!-- target bean to be referenced by name --> <bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype"> <property name="age" value="10"/> <property name="spouse"> <bean class="org.springframework.beans.TestBean"> <property name="age" value="11"/> </bean> </property> </bean>
Before…
<!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' --> <bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> </list> </property> </bean>
The above configuration uses a Spring FactoryBean implementation, the ListFactoryBean, to create a java.util.List instance initialized with values taken from the supplied ‘sourceList’.
After…
<!-- creates a java.util.List instance with the supplied values --> <util:list id="emails"> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> </util:list>
You can also explicitly control the exact type of List that will be instantiated and populated via the use of the ‘list-class’ attribute on the element. For example, if we really need a java.util.LinkedList to be instantiated, we could use the following configuration:
<util:list id="emails" list-class="java.util.LinkedList"> <value>jackshaftoe@vagabond.org</value> <value>eliza@thinkingmanscrumpet.org</value> <value>vanhoek@pirate.org</value> <value>d'Arcachon@nemesis.org</value> </util:list>
If no ‘list-class’ attribute is supplied, a List implementation will be chosen by the container.
Before…
<!-- creates a java.util.Map instance with values loaded from the supplied 'sourceMap' --> <bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="sourceMap"> <map> <entry key="pechorin" value="pechorin@hero.org"/> <entry key="raskolnikov" value="raskolnikov@slums.org"/> <entry key="stavrogin" value="stavrogin@gov.org"/> <entry key="porfiry" value="porfiry@gov.org"/> </map> </property> </bean>
The above configuration uses a Spring FactoryBean implementation, the MapFactoryBean, to create a java.util.Map instance initialized with key-value pairs taken from the supplied ‘sourceMap’.
After…
<!-- creates a java.util.Map instance with the supplied key-value pairs --> <util:map id="emails"> <entry key="pechorin" value="pechorin@hero.org"/> <entry key="raskolnikov" value="raskolnikov@slums.org"/> <entry key="stavrogin" value="stavrogin@gov.org"/> <entry key="porfiry" value="porfiry@gov.org"/> </util:map>
You can also explicitly control the exact type of Map that will be instantiated and populated via the use of the ‘map-class’ attribute on the element. For example, if we really need a java.util.TreeMap to be instantiated, we could use the following configuration:
<util:map id="emails" map-class="java.util.TreeMap"> <entry key="pechorin" value="pechorin@hero.org"/> <entry key="raskolnikov" value="raskolnikov@slums.org"/> <entry key="stavrogin" value="stavrogin@gov.org"/> <entry key="porfiry" value="porfiry@gov.org"/> </util:map>
If no ‘map-class’ attribute is supplied, a Map implementation will be chosen by the container.
Before…
<!-- creates a java.util.Set instance with values loaded from the supplied 'sourceSet' --> <bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean"> <property name="sourceSet"> <set> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> </set> </property> </bean>
The above configuration uses a Spring FactoryBean implementation, the SetFactoryBean, to create a java.util.Set instance initialized with values taken from the supplied ‘sourceSet’.
After…
<!-- creates a java.util.Set instance with the supplied values --> <util:set id=< 1069e span class="hljs-value">"emails"> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> </util:set>
You can also explicitly control the exact type of Set that will be instantiated and populated via the use of the ‘set-class’ attribute on the element. For example, if we really need a java.util.TreeSet to be instantiated, we could use the following configuration:
<util:set id="emails" set-class="java.util.TreeSet"> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> </util:set>
If no ‘set-class’ attribute is supplied, a Set implementation will be chosen by the container.
C.2.3 The jee schema
The jee tags deal with Java EE (Java Enterprise Edition)-related configuration issues, such as looking up a JNDI object and defining EJB references.
To use the tags in the jee schema, you need to have the following preamble at the top of your Spring XML configuration file; the bold text in the following snippet references the correct schema so that the tags in the jee namespace are available to you.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> <!-- <bean/> definitions here --> </beans>
C.2.3.1 (simple)
Before…
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/MyDataSource"/> </bean> <bean id="userDao" class="com.foo.JdbcUserDao"> <!-- Spring will do the cast automatically (as usual) --> <property name="dataSource" ref="dataSource"/> </bean> After... <jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/> <bean id="userDao" class="com.foo.JdbcUserDao"> <!-- Spring will do the cast automatically (as usual) --> <property name="dataSource" ref="dataSource"/> </bean>
C.2.3.2 (with single JNDI environment setting)
Before…
<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/MyDataSource"/> <property name="jndiEnvironment"> <props> <prop key="foo">bar</prop> </props> </property> </bean>
After…
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource"> <jee:environment>foo=bar</jee:environment> </jee:jndi-lookup>
(with multiple JNDI environment settings)
Before…
<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/MyDataSource"/> <property name="jndiEnvironment"> <props> <prop key="foo">bar</prop> <prop key="ping">pong</prop> </props> </property> </bean>
After…
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource"> <!-- newline-separated, key-value pairs for the environment (standard Properties format) --> <jee:environment> foo=bar ping=pong </jee:environment> </jee:jndi-lookup>
参考此链接
相关文章推荐
- Spring基于xml配置文件对list、set、map集合的属性注入
- spring List,Set,Map,Properties,array的配置文件注入方式
- Spring中注入List Set Map Properties的配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring 3配置文件中如何注入map list set等类型
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring 配置文件 list map set
- spring 配置文件中如何注入map list set等类型
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring 3配置文件中如何注入map list set等类型
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- List,Set,Map,Properties的Spring注入实例
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据