![]() |
This appendix details the XML Schema-based configuration introduced in Spring 2.0 and enhanced and extended in Spring 2.5 and 3.0. The central motivation for moving to XML Schema based configuration files was to make
Spring XML configuration easier. The 'classic' From the Spring IoC containers point-of-view, everything is a bean. That’s great
news for the Spring IoC container, because if everything is a bean then everything can
be treated in the exact same fashion. Spring 2.0’s new XML Schema-based configuration addresses this issue. The The key thing to remember is that the new custom tags work best for infrastructure or
integration beans: The examples included below will hopefully convince you that the inclusion of XML Schema
support in Spring 2.0 was a good idea. To switch over from the DTD-style to the new XML Schema-style, you need to make the following change. <?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"> The equivalent file in the XML Schema-style would be… <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
The above Spring XML configuration fragment is boilerplate that you can copy and paste
(!) and then plug The rest of this chapter is devoted to showing examples of the new Spring XML Schema
based configuration, First up is coverage of the To use the tags in the <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 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>
Find below an example which shows how a <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 <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> This does mean that there is no longer any choice in what the bean id is (so any other bean that refers to it will also have to use this longer name), but this form is very concise to define, and very convenient to use as an inner bean since the id doesn’t have to be specified for the bean reference: <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 Injecting enum values into beans as either property or constructor arguments is very
easy to do in Spring, in that you don’t package javax.persistence; public enum PersistenceContextType { TRANSACTION, EXTENDED } Now consider a setter of type package example; public class Client { private PersistenceContextType persistenceContextType; public void setPersistenceContextType(PersistenceContextType type) { this.persistenceContextType = type; } }
<bean class="example.Client"> <property name="persistenceContextType" value="TRANSACTION" /> </bean> This works for classic type-safe emulated enums (on 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 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> <!-- will result in 10, which is the value of property age of bean testBean --> <util:property-path id="name" path="testBean.age"/> The value of the
Here’s an example where a path is used against another bean, by name: // target bean to be referenced by name <bean id="person" 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 11, which is the value of property spouse.age of bean person <bean id="theAge" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="person"/> <property name="propertyPath" value="spouse.age"/> </bean> In this example, a path is evaluated against an inner bean: <!-- will result in 12, which is the value of property age of the inner bean --> <bean id="theAge" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetObject"> <bean class="org.springframework.beans.TestBean"> <property name="age" value="12"/> </bean> </property> <property name="propertyPath" value="age"/> </bean> There is also a shortcut form, where the bean name is the property path. <!-- will result in 10, which is the value of property age of bean person --> <bean id="person.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> This form does mean that there is no choice in the name of the bean. Any reference to it will also have to use the same id, which is the path. Of course, if used as an inner bean, there is no need to refer to it at all: <bean id="..." class="..."> <property name="age"> <bean id="person.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> </property> </bean> The result type may be specifically set in the actual definition. This is not necessary for most use cases, but can be of use for some. Please see the Javadocs for more info on this feature. Before… <!-- creates a java.util.Properties instance with values loaded from the supplied location --> <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:com/foo/jdbc-production.properties"/> </bean> The above configuration uses a Spring After… <!-- creates a java.util.Properties instance with values loaded from the supplied location --> Before… <!-- creates a java.util.List instance with values loaded from the supplied The above configuration uses a Spring After… <!-- creates a java.util.List instance with the supplied values --> <util:list id="emails"> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </util:list> You can also explicitly control the exact type of <util:list id="emails" list-class="java.util.LinkedList"> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>d'[email protected]</value> </util:list> If no 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="[email protected]"/> <entry key="raskolnikov" value="[email protected]"/> <entry key="stavrogin" value="[email protected]"/> <entry key="porfiry" value="[email protected]"/> </map> </property> </bean> The above configuration uses a Spring After… <!-- creates a java.util.Map instance with the supplied key-value pairs --> <util:map id="emails"> <entry key="pechorin" value="[email protected]"/> <entry key="raskolnikov" value="[email protected]"/> <entry key="stavrogin" value="[email protected]"/> <entry key="porfiry" value="[email protected]"/> </util:map> You can also explicitly control the exact type of <util:map id="emails" map-class="java.util.TreeMap"> <entry key="pechorin" value="[email protected]"/> <entry key="raskolnikov" value="[email protected]"/> <entry key="stavrogin" value="[email protected]"/> <entry key="porfiry" value="[email protected]"/> </util:map> If no 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>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </set> </property> </bean> The above configuration uses a Spring After… <!-- creates a java.util.Set instance with the supplied values --> <util:set id="emails"> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </util:set> You can also explicitly control the exact type of <util:set id="emails" set-class="java.util.TreeSet"> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </util:set> If no The To use the tags in the <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> 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> 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> Before… <bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/MyDataSource"/> <property name="cache" value="true"/> <property name="resourceRef" value="true"/> <property name="lookupOnStartup" value="false"/> <property name="expectedType" value="com.myapp.DefaultFoo"/> <property name="proxyInterface" value="com.myapp.Foo"/> </bean> After… <jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource" cache="true" resource-ref="true" lookup-on-startup="false" expected-type="com.myapp.DefaultFoo" proxy-interface="com.myapp.Foo"/> The Before… <bean id="simple" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean"> <property name="jndiName" value="ejb/RentalServiceBean"/> <property name="businessInterface" value="com.foo.service.RentalService"/> </bean> After… <jee:local-slsb id="simpleSlsb" jndi-name="ejb/RentalServiceBean" business-interface="com.foo.service.RentalService"/> <bean id="complexLocalEjb" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean"> <property name="jndiName" value="ejb/RentalServiceBean"/> <property name="businessInterface" value="com.foo.service.RentalService"/> <property name="cacheHome" value="true"/> <property name="lookupHomeOnStartup" value="true"/> <property name="resourceRef" value="true"/> </bean> After… <jee:local-slsb id="complexLocalEjb" jndi-name="ejb/RentalServiceBean" business-interface="com.foo.service.RentalService" cache-home="true" lookup-home-on-startup="true" resource-ref="true"> The Before… <bean id="complexRemoteEjb" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean"> <property name="jndiName" value="ejb/MyRemoteBean"/> <property name="businessInterface" value="com.foo.service.RentalService"/> <property name="cacheHome" value="true"/> <property name="lookupHomeOnStartup" value="true"/> <property name="resourceRef" value="true"/> <property name="homeInterface" value="com.foo.service.RentalService"/> <property name="refreshHomeOnConnectFailure" value="true"/> </bean> After… <jee:remote-slsb id="complexRemoteEjb" jndi-name="ejb/MyRemoteBean" business-interface="com.foo.service.RentalService" cache-home="true" lookup-home-on-startup="true" resource-ref="true" home-interface="com.foo.service.RentalService" refresh-home-on-connect-failure="true"> The These tags (and the dynamic language support) are comprehensively covered in the chapter
entitled Chapter 28, Dynamic language support. Please do consult that chapter for full details on this In the interest of completeness, to use the tags in the <?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:lang="http://www.springframework.org/schema/lang" The In the interest of completeness, to use the tags in the <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" The
In the interest of completeness, to use the tags in the <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
The In the interest of completeness, to use the tags in the <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" The <?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:context="http://www.springframework.org/schema/context"
This element activates the replacement of Activates the Spring infrastructure for various annotations to be detected in bean
classes: Spring’s
This element is detailed in Section 4.9, “Annotation-based container configuration”. This element is detailed in Section 8.8.4, “Load-time weaving with AspectJ in the Spring Framework”. This element is detailed in Section 8.8.1, “Using AspectJ to dependency inject domain objects with Spring”. This element is detailed in Section 24.4.3, “Configuring annotation based MBean export”. The The The To use the tags in the <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" The To use the tags in the <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Last but not least we have the tags in the One thing that is new to the beans tags themselves in Spring 2.0 is the idea of
arbitrary bean metadata. Find below an example of the <?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.xsd"> <bean id="foo" class="x.y.Foo"> <meta key="cacheName" value="foo"/> <property name="name" value="Rick"/> </bean> </beans> In the case of the above example, you would assume that there is some logic that will
|