XML Schema-based configuration
XML Schema-based configuration34.1IntroductionThis 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,everythingis 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. The same,however,is not true from a developer’s point-of-view. The objects defined in a Spring XML configuration file are not all generic,vanilla beans. Usually,each bean requires some degree of specific configuration. 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: for example,AOP,collections,transactions,integration with 3rd-party frameworks such as Mule,etc.,while the existing bean tags are best suited to application-specific beans,such as DAOs,service layer objects,validators,etc. The examples included below will hopefully convince you that the inclusion of XML Schema support in Spring 2.0 was a good idea. The reception in the community has been encouraging; also,please note the fact that this new configuration mechanism is totally customisable and extensible. This means you can write your own domain-specific configuration tags that would better represent your application’s domain; the process involved in doing so is covered in the appendix entitledChapter35,Extensible XML authoring. 34.2XML Schema-based configuration
34.2.1Referencing the schemasTo 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"> <beans> <!-- bean definitions here --> </beans> 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
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,with at least one example for every new tag. The format follows a before and after style,with abeforesnippet of XML showing the old (but still 100% legal and supported) style,followed immediately by anafterexample showing the equivalent in the new XML Schema-based style. 34.2.2the util schemaFirst up is coverage of the To use the tags in the "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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> </beans> <util:constant/>Before… <bean id="..." class="..."> <property name="isolation"> "java.sql.Connection.TRANSACTION_SERIALIZABLE" "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. <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> </bean> Setting a bean property or constructor arg from a field value
Find below an example which shows how a "myField" "staticField" value=/> </bean> There is also a convenience usage form where the "java.sql.Connection.TRANSACTION_SERIALIZABLE"
/>
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> It is also possible to access a non-static (instance) field of another bean,as described in the API documentation for theFieldRetrievingFactoryBeanclass. 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 todoanything or know anything about the Spring internals (or even about classes such as the package javax.persistence; public enum PersistenceContextType { TRANSACTION,EXTENDED } Now consider a setter of type package example; public class Client { private PersistenceContextType persistenceContextType; void setPersistenceContextType(PersistenceContextType type) { this.persistenceContextType = type; } }
"example.Client""persistenceContextType" "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. <util:property-path/>Before… <!-- target bean to be referenced by name --> "testBean" "org.springframework.beans.TestBean" scope="prototype""age" "10""spouse""org.springframework.beans.TestBean"> "11"/> </bean> </bean> <!-- will result in 10,which is the value of property age of bean testBean --> "testBean.age" "org.springframework.beans.factory.config.PropertyPathFactoryBean"/> The above configuration uses a Spring After… <util:property-path "name" path="testBean.age"/> The value of the Using <util:property-path/> to set a bean property or constructor-argument
Here’s an example where a path is used against another bean,by name: // target bean to be referenced by name "person" </bean> // will result in 11,which is the value of property spouse.age of bean person "theAge" "targetBeanName" "person""propertyPath" "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 --> "targetObject""12"</property> "age"</bean> There is also a shortcut form,where the bean name is the property path. age of bean person --> "person.age" /> 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: "person.age"
</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. <util:properties/>Before… <!-- creates a java.util.Properties instance with values loaded from the supplied location --> "jdbcConfiguration" "org.springframework.beans.factory.config.PropertiesFactoryBean""location" "classpath:com/foo/jdbc-production.properties"PropertiesFactoryBean,to instantiate a <util:list/>Before… <!-- creates a java.util.List instance with values loaded from the supplied sourceList --> "emails" "org.springframework.beans.factory.config.ListFactoryBean""sourceList"<list> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org<value>stavrogin@gov.org<value>porfiry@gov.org</value> </list> ListFactoryBean,to create a You can also explicitly control the exact type of list-class="java.util.LinkedList"<value>jackshaftoe@vagabond.org<value>eliza@thinkingmanscrumpet.org<value>vanhoek@pirate.org<value>d'Arcachon@nemesis.org</util:list> If no <util:map/>Before… <!-- creates a java.util.Map instance with values loaded from the supplied sourceMap --> "org.springframework.beans.factory.config.MapFactoryBean""sourceMap"<map> <entry key="pechorin" "pechorin@hero.org"/> "raskolnikov" "raskolnikov@slums.org""stavrogin" "stavrogin@gov.org""porfiry" "porfiry@gov.org"</map> MapFactoryBean,242)">java.util.Mapinstance initialized with key-value pairs taken from the supplied You can also explicitly control the exact type of If no <util:set/>Before… <!-- creates a java.util.Set instance with values loaded from the supplied sourceSet --> "org.springframework.beans.factory.config.SetFactoryBean""sourceSet"<set> </set> SetFactoryBean,242)">java.util.Setinstance initialized with values taken from the supplied You can also explicitly control the exact type of If no 34.2.3the jee schemaThe To use the tags in the "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.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <jee:jndi-lookup/> (simple) Before… "dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> "jndiName" "jdbc/MyDataSource"</bean> "userDao" "com.foo.JdbcUserDao"> <!-- Spring will do the cast automatically (as usual) --> "dataSource" ref="dataSource"/> </bean> After… <jee:jndi-lookup "dataSource" jndi-name="jdbc/MyDataSource"/> </bean> <jee:jndi-lookup/> (with single JNDI environment setting)Before… "simple" "org.springframework.jndi.JndiObjectFactoryBean""jndiEnvironment"<props> <prop "foo">bar</prop> </props> jndi-name=<jee:environment>foo=bar</jee:environment> </jee:jndi-lookup> <jee:jndi-lookup/> (with multiple JNDI environment settings)Before… </prop> "ping">pong<!-- newline-separated,key-value pairs for the environment (standard Properties format) --> <jee:environment> foo=bar ping=pong <jee:jndi-lookup/> (complex) Before… "cache" "true""resourceRef" "lookupOnStartup" "false""expectedType" "com.myapp.DefaultFoo""proxyInterface" "com.myapp.Foo""simple" "jdbc/MyDataSource" cache="true" resource-ref=lookup-on-startup="false" expected-type="com.myapp.DefaultFoo" proxy-interface= <jee:local-slsb/> (simple) <jee:remote-slsb/>The Before… "complexRemoteEjb" "org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean""ejb/MyRemoteBean""homeInterface" "refreshHomeOnConnectFailure" <jee:remote-slsb "ejb/MyRemoteBean" home-interface=refresh-home-on-connect-failure=> 34.2.4the lang schemaThe These tags (and the dynamic language support) are comprehensively covered in the chapter entitledChapter29,Dynamic language support. Please do consult that chapter for full details on this support and the In the interest of completeness,to use the tags in the "http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> </beans> 34.2.5the jms schemaThe In the interest of completeness,242)">jmsschema,242)">jmsnamespace are available to you. "http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> 34.2.6the tx (transaction) schema The
In the interest of completeness,242)">txschema,242)">txnamespace are available to you. "http://www.springframework.org/schema/beans" "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> Often when using the tags in the |
34.2.7the aop schema
Theaop
tags deal with configuring all things AOP in Spring: this includes Spring’s own proxy-based AOP framework and Spring’s integration with the AspectJ AOP framework. These tags are comprehensively covered in the chapter entitledChapter9,Aspect Oriented Programming with Spring.
In the interest of completeness,242)">aopschema,242)">aopnamespace are available to you.
"http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 34.2.8the context schema