四.Spring的schema模板

2024-06-09 12:48
文章标签 java 模板 spring schema

本文主要是介绍四.Spring的schema模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在从官网下载下来的release项目中spring-framework-4.3.2.RELEASE解压后在spring-framework-4.3.2.RELEASE\docs\spring-framework-reference\html下的
xsd-configuration.html文件是官方提供的各个模块与之对应的schema模板样式.

这里以spring-beans-4.3.2.RELEASE.jar项目需要schema的约束为例子:
我们需要找的是 the beans 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.xsd"><bean id="foo" class="x.y.Foo"><meta key="cacheName" value="foo"/><property name="name" value="Rick"/></bean>
</beans>
原文如下:

41. XML Schema-based configuration

41.1 Introduction

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’ <bean/>-based approach is good, but its generic-nature comes with a price in terms of configuration overhead.

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. 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 <bean/> element is still present, and if you wanted to, you could continue to write the exact same style of Spring XML configuration using only <bean/> elements. The new XML Schema-based configuration does, however, make Spring XML configuration files substantially clearer to read. In addition, it allows you to express the intent of a bean definition.

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 entitled Chapter 42, Extensible XML authoring.

41.2 XML Schema-based configuration

41.2.1 Referencing the schemas

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"><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"><!-- bean definitions here --></beans>
[Note]Note

The 'xsi:schemaLocation' fragment is not actually required, but can be included to reference a local copy of a schema (which can be useful during development).

The above Spring XML configuration fragment is boilerplate that you can copy and paste (!) and then plug <bean/> definitions into like you have always done. However, the entire point of switching over is to take advantage of the new Spring 2.0 XML tags since they make configuration easier. The section entitled Section 41.2.2, “the util schema” demonstrates how you can start immediately by using some of the more common utility tags.

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 a before snippet of XML showing the old (but still 100% legal and supported) style, followed immediately by an after example showing the equivalent in the new XML Schema-based style.

41.2.2 the util schema

First up is coverage of the util tags. As the name implies, the util tags deal with common, utility configuration issues, such as configuring collections, referencing constants, and suchlike.

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 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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here --></beans>
<util:constant/>

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 (unnecessarily) 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="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 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;}}
  1. and the corresponding bean definition:
<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.

<util:property-path/>

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><!-- 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 path attribute of the <property-path/> tag follows the form beanName.beanProperty.

Using <util:property-path/> to set a bean property or constructor-argument

PropertyPathFactoryBean is a FactoryBean that evaluates a property path on a given target object. The target object can be specified directly or via a bean name. This value may then be used in another bean definition as a property value or constructor argument.

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.

<util:properties/>

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 FactoryBean implementation, the PropertiesFactoryBean, to instantiate a java.util.Properties instance with values loaded from the supplied Resource location).

After…​

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>
<util:list/>

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 <util:list/> 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.

<util:map/>

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 <util:map/> 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.

<util:set/>

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="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 <util:set/> 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.

41.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 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.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- bean definitions here --></beans>
<jee:jndi-lookup/> (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>
<jee:jndi-lookup/> (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>
<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=barping=pong</jee:environment>
</jee:jndi-lookup>
<jee:jndi-lookup/> (complex)

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"/>
<jee:local-slsb/> (simple)

The <jee:local-slsb/> tag configures a reference to an EJB Stateless SessionBean.

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"/>
<jee:local-slsb/> (complex)
<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">
<jee:remote-slsb/>

The <jee:remote-slsb/> tag configures a reference to a remote EJB Stateless SessionBean.

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">

41.2.4 the lang schema

The lang tags deal with exposing objects that have been written in a dynamic language such as JRuby or Groovy as beans in the Spring container.

These tags (and the dynamic language support) are comprehensively covered in the chapter entitled Chapter 35, Dynamic language support. Please do consult that chapter for full details on this support and the lang tags themselves.

In the interest of completeness, to use the tags in the lang schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the lang 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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <!-- bean definitions here --></beans>

41.2.5 the jms schema

The jms tags deal with configuring JMS-related beans such as Spring’s MessageListenerContainers. These tags are detailed in the section of the JMS chapter entitled Section 30.7, “JMS Namespace Support”. Please do consult that chapter for full details on this support and the jms tags themselves.

In the interest of completeness, to use the tags in the jms schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the jms 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:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> <!-- bean definitions here --></beans>

41.2.6 the tx (transaction) schema

The tx tags deal with configuring all of those beans in Spring’s comprehensive support for transactions. These tags are covered in the chapter entitled Chapter 17, Transaction Management.

[Tip]Tip

You are strongly encouraged to look at the 'spring-tx.xsd' file that ships with the Spring distribution. This file is (of course), the XML Schema for Spring’s transaction configuration, and covers all of the various tags in the tx namespace, including attribute defaults and suchlike. This file is documented inline, and thus the information is not repeated here in the interests of adhering to the DRY (Don’t Repeat Yourself) principle.

In the interest of completeness, to use the tags in the tx schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the tx 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: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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --></beans>
[Note]Note

Often when using the tags in the tx namespace you will also be using the tags from the aop namespace (since the declarative transaction support in Spring is implemented using AOP). The above XML snippet contains the relevant lines needed to reference the aop schema so that the tags in the aop namespace are available to you.

41.2.7 the aop schema

The aop 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 entitled Chapter 11, Aspect Oriented Programming with Spring.

In the interest of completeness, to use the tags in the aop schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the aop 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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --></beans>

41.2.8 the context schema

The context tags deal with ApplicationContext configuration that relates to plumbing - that is, not usually beans that are important to an end-user but rather beans that do a lot of grunt work in Spring, such as BeanfactoryPostProcessors. The following snippet references the correct schema so that the tags in the context 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --></beans>
[Note]Note

The context schema was only introduced in Spring 2.5.

<property-placeholder/>

This element activates the replacement of ${…​} placeholders, resolved against the specified properties file (as a Spring resource location). This element is a convenience mechanism that sets up aPropertyPlaceholderConfigurer for you; if you need more control over the PropertyPlaceholderConfigurer, just define one yourself explicitly.

<annotation-config/>

Activates the Spring infrastructure for various annotations to be detected in bean classes: Spring’s @Required and @Autowired, as well as JSR 250’s @PostConstruct, @PreDestroy and @Resource (if available), and JPA’s @PersistenceContext and @PersistenceUnit (if available). Alternatively, you can choose to activate the individual BeanPostProcessors for those annotations explicitly.

[Note]Note

This element does not activate processing of Spring’s @Transactional annotation. Use the <tx:annotation-driven/> element for that purpose.

<component-scan/>

This element is detailed in Section 7.9, “Annotation-based container configuration”.

<load-time-weaver/>

This element is detailed in Section 11.8.4, “Load-time weaving with AspectJ in the Spring Framework”.

<spring-configured/>

This element is detailed in Section 11.8.1, “Using AspectJ to dependency inject domain objects with Spring”.

<mbean-export/>

This element is detailed in Section 31.4.3, “Configuring annotation based MBean export”.

41.2.9 the tool schema

The tool tags are for use when you want to add tooling-specific metadata to your custom configuration elements. This metadata can then be consumed by tools that are aware of this metadata, and the tools can then do pretty much whatever they want with it (validation, etc.).

The tool tags are not documented in this release of Spring as they are currently undergoing review. If you are a third party tool vendor and you would like to contribute to this review process, then do mail the Spring mailing list. The currently supported tool tags can be found in the file 'spring-tool.xsd' in the 'src/org/springframework/beans/factory/xml' directory of the Spring source distribution.

41.2.10 the jdbc schema

The jdbc tags allow you to quickly configure an embedded database or initialize an existing data source. These tags are documented in Section 19.8, “Embedded database support” and Section 19.9, “Initializing a DataSource” respectively.

To use the tags in the jdbc schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the jdbc 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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> <!-- bean definitions here --></beans>

41.2.11 the cache schema

The cache tags can be used to enable support for Spring’s @CacheEvict, @CachePut and @Caching annotations. It it also supports declarative XML-based caching. See Section 36.3.6, “Enable caching annotations” and Section 36.5, “Declarative XML-based caching” for details.

To use the tags in the cache schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the cache 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:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- bean definitions here --></beans>

41.2.12 the beans schema

Last but not least we have the tags in the beans schema. These are the same tags that have been in Spring since the very dawn of the framework. Examples of the various tags in the beans schema are not shown here because they are quite comprehensively covered in Section 7.4.2, “Dependencies and configuration in detail” (and indeed in that entire chapter).

One thing that is new to the beans tags themselves in Spring 2.0 is the idea of arbitrary bean metadata. In Spring 2.0 it is now possible to add zero or more key / value pairs to <bean/> XML definitions. What, if anything, is done with this extra metadata is totally up to your own custom logic (and so is typically only of use if you are writing your own custom tags as described in the appendix entitled Chapter 42, Extensible XML authoring).

Find below an example of the <meta/> tag in the context of a surrounding <bean/> (please note that without any logic to interpret it the metadata is effectively useless as-is).

<?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 consume the bean definition and set up some caching infrastructure using the supplied metadata.


这篇关于四.Spring的schema模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1045225

相关文章

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进