基于xml schema的扩展标签

xml schema是spring 2.0版本之后引入的,在之后的2.5和3.x加入了新的元素。引入的主要动机在于:虽说spring把<bean/>中一切皆为对象,但在开发人员的角度上讲,我们要在Spring中具体化或抽象化一些东西,比如具体化单值、集合;或特定于具体应用的抽象比如AOP,事务。那不得不在spring中配置一些基础设施bean。或第三方框架支持我们都使用过spring Security框架,说实在的如果不使用security标签,我们必须为每个过滤器有一个<bean/>定义。所以我们为了方便不得不去自定义标签,xml schema将适用。

引入schema
  dtd配置
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"   
  3.     "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  4.   
  5. <beans>  
  6.   
  7. <!-- bean definitions here -->  
  8.   
  9. </beans>  
<?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>

   schema配置
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="   
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.   
  7. <!-- bean definitions here -->  
  8.   
  9. </beans>  
<?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>

以上配置复制粘贴就行,其中schemaLocation不是必须的,但在开发可以通过他在jar中找到spring-beans.xsd。接下来看下spring中提供的自定义标签.

util schema
主要处理集合,对象或类域。使用之前,配置schema头
 
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:util="http://www.springframework.org/schema/util"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10.   
  11. </beans>  
  12.    
<?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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!-- bean definitions here -->

</beans>
 

<util:constant/>
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="..." class="...">  
  2.   <property name="isolation">  
  3.     <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"  
  4.     class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />  
  5.   </property>  
  6. </bean>  
<bean id="..." class="...">
  <property name="isolation">
    <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
    class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
  </property>
</bean>

比较复杂的构造基本通过FactoryBean实现,FieldRetrievingFactoryBean检索类或对象的字段。
基于schema的配置
Xml代码 复制代码  收藏代码
  1. <bean id="..." class="...">  
  2.   <property name="isolation">  
  3.     <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>  
  4.   </property>  
  5. </bean>  
<bean id="..." class="...">
  <property name="isolation">
    <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
  </property>
</bean>


<util:property-path/>
使用之前
Xml代码 复制代码  收藏代码
  1. <!-- target bean to be referenced by name -->  
  2. <bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">  
  3.   <property name="age" value="10"/>  
  4.   <property name="spouse">  
  5.     <bean class="org.springframework.beans.TestBean">  
  6.       <property name="age" value="11"/>  
  7.     </bean>  
  8.   </property>  
  9. </bean>  
  10.   
  11. <!-- will result in 10, which is the value of property 'age' of bean 'testBean' -->  
  12. <bean id="testBean.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>  
<!-- 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"/>

使用FactoryBean实现,PropertyPathFactoryBean创建一个ID为“testBean.age”的Bean,整型10。
基于schema配置
Xml代码 复制代码  收藏代码
  1. <!-- target bean to be referenced by name -->  
  2. <bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">  
  3.   <property name="age" value="10"/>  
  4.   <property name="spouse">  
  5.     <bean class="org.springframework.beans.TestBean">  
  6.       <property name="age" value="11"/>  
  7.     </bean>  
  8.   </property>  
  9. </bean>  
  10.   
  11. <!-- will result in 10, which is the value of property 'age' of bean 'testBean' -->  
  12. <util:property-path id="name" path="testBean.age"/>  
<!-- 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"/>


<util:properties/>
使用之前
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.Properties instance with values loaded from the supplied location -->  
  2. <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.   <property name="location" value="classpath:com/foo/jdbc-production.properties"/>  
  4. </bean>  
<!-- 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>

PropertiesFactoryBean实例化java.util.Properties,载入location指定Resource资源。
基于schema设置
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.Properties instance with values loaded from the supplied location -->  
  2. <util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>  
<!-- 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/>
使用之前
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' -->  
  2. <bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean">  
  3.   <property name="sourceList">  
  4.       <list>  
  5.         <value>[email protected]</value>  
  6.         <value>[email protected]</value>  
  7.         <value>[email protected]</value>  
  8.         <value>[email protected]</value>  
  9.       </list>  
  10.   </property>  
  11. </bean>  
<!-- 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>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
      </list>
  </property>
</bean>

ListFactoryBean创建了List集合
基于schema配置
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.List instance with the supplied values -->  
  2. <util:list id="emails" list-class="java.util.LinkedList">  
  3.     <value>[email protected]</value>  
  4.     <value>[email protected]</value>  
  5.     <value>[email protected]</value>  
  6.     <value>[email protected]</value>  
  7. </util:list>  
<!-- creates a java.util.List instance with the supplied values -->
<util:list id="emails" list-class="java.util.LinkedList">
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
</util:list>


<util:map/>
使用之前
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.Map instance with values loaded from the supplied 'sourceMap' -->  
  2. <bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean">  
  3.   <property name="sourceMap">  
  4.       <map>  
  5.         <entry key="pechorin" value="[email protected]"/>  
  6.         <entry key="raskolnikov" value="[email protected]"/>  
  7.         <entry key="stavrogin" value="[email protected]"/>  
  8.         <entry key="porfiry" value="[email protected]"/>  
  9.       </map>  
  10.   </property>  
  11. </bean>  
<!-- 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>

MapFactoryBean创建了一个Map
基于schema配置
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.Map instance with the supplied key-value pairs -->  
  2. <util:map id="emails" map-class="java.util.TreeMap">  
  3.     <entry key="pechorin" value="[email protected]"/>  
  4.     <entry key="raskolnikov" value="[email protected]"/>  
  5.     <entry key="stavrogin" value="[email protected]"/>  
  6.     <entry key="porfiry" value="[email protected]"/>  
  7. </util:map>  
<!-- creates a java.util.Map instance with the supplied key-value pairs -->
<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>


<util:set/>
使用之前
Xml代码 复制代码  收藏代码
  1. <!-- creates a java.util.Set instance with values loaded from the supplied 'sourceSet' -->  
  2. <bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean">  
  3.   <property name="sourceSet">  
  4.       <set>  
  5.         <value>[email protected]</value>  
  6.         <value>[email protected]</value>  
  7.         <value>[email protected]</value>  
  8.         <value>[email protected]</value>  
  9.       </set>  
  10.   </property>  
  11. </bean>  
<!-- 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>

SetFactoryBean创建了 java.util.Set实例
基于schema配置
Xml代码 复制代码  收藏代码
  1. <util:set id="emails" set-class="java.util.TreeSet">  
  2.     <value>[email protected]</value>  
  3.     <value>[email protected]</value>  
  4.     <value>[email protected]</value>  
  5.     <value>[email protected]</value>  
  6. </util:set>  
<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>


jee schema
jee 标签主要查找JNDI Object比如 数据源,EJB引用。
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:jee="http://www.springframework.org/schema/jee"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10.   
  11. </beans>  
<?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.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

<!-- bean definitions here -->

</beans>

<jee:jndi-lookup/> 简单
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  
  2.     <property name="jndiName" value="jdbc/MyDataSource"/>  
  3. </bean>  
  4.   
  5. <bean id="userDao" class="com.foo.JdbcUserDao">  
  6.     <!-- Spring will do the cast automatically (as usual) -->  
  7.     <property name="dataSource" ref="dataSource"/>  
  8. </bean  
<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

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>  
  2.   
  3. <bean id="userDao" class="com.foo.JdbcUserDao">  
  4.     <!-- Spring will do the cast automatically (as usual) -->  
  5.     <property name="dataSource" ref="dataSource"/>  
  6. </bean>  
<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/> 单环境设置
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">  
  2.     <property name="jndiName" value="jdbc/MyDataSource"/>  
  3.     <property name="jndiEnvironment">  
  4.         <props>  
  5.             <prop key="foo">bar</prop>  
  6.         </props>  
  7.     </property>  
  8. </bean>  
<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>

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">  
  2.     <jee:environment>foo=bar</jee:environment>  
  3. </jee:jndi-lookup>  
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
    <jee:environment>foo=bar</jee:environment>
</jee:jndi-lookup>


<jee:jndi-lookup/> 多环境设置
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">  
  2.     <property name="jndiName" value="jdbc/MyDataSource"/>  
  3.     <property name="jndiEnvironment">  
  4.         <props>  
  5.             <prop key="foo">bar</prop>  
  6.             <prop key="ping">pong</prop>  
  7.         </props>  
  8.     </property>  
  9. </bean>  
<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>

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">  
  2.     <jee:environment>  
  3.         foo=bar  
  4.         ping=pong</jee:environment>  
  5. </jee:jndi-lookup>  
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
    <jee:environment>
        foo=bar
        ping=pong</jee:environment>
</jee:jndi-lookup>


<jee:jndi-lookup/> 复杂设置
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">  
  2.     <property name="jndiName" value="jdbc/MyDataSource"/>  
  3.     <property name="cache" value="true"/>  
  4.     <property name="resourceRef" value="true"/>  
  5.     <property name="lookupOnStartup" value="false"/>  
  6.     <property name="expectedType" value="com.myapp.DefaultFoo"/>  
  7.     <property name="proxyInterface" value="com.myapp.Foo"/>  
  8. </bean>  
<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>

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:jndi-lookup id="simple"  
  2.              jndi-name="jdbc/MyDataSource"  
  3.              cache="true"  
  4.              resource-ref="true"  
  5.              lookup-on-startup="false"  
  6.              expected-type="com.myapp.DefaultFoo"  
  7.              proxy-interface="com.myapp.Foo"/>  
<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/> 简单
<jee:local-slsb/>配置一个本地EJB无状态会话BEAN
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="simple"  
  2.       class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">  
  3.   <property name="jndiName" value="ejb/RentalServiceBean"/>  
  4.   <property name="businessInterface" value="com.foo.service.RentalService"/>  
  5. </bean>  
<bean id="simple"
      class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
  <property name="jndiName" value="ejb/RentalServiceBean"/>
  <property name="businessInterface" value="com.foo.service.RentalService"/>
</bean>

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:local-slsb id="simpleSlsb" jndi-name="ejb/RentalServiceBean"  
  2.     business-interface="com.foo.service.RentalService"/>  
<jee:local-slsb id="simpleSlsb" jndi-name="ejb/RentalServiceBean"
    business-interface="com.foo.service.RentalService"/>


<jee:local-slsb/>复杂配置
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="complexLocalEjb"  
  2.       class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">  
  3.   <property name="jndiName" value="ejb/RentalServiceBean"/>  
  4.   <property name="businessInterface" value="com.foo.service.RentalService"/>  
  5.   <property name="cacheHome" value="true"/>  
  6.   <property name="lookupHomeOnStartup" value="true"/>  
  7.   <property name="resourceRef" value="true"/>  
  8. </bean>  
<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>

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:local-slsb id="complexLocalEjb"  
  2.     jndi-name="ejb/RentalServiceBean"  
  3.     business-interface="com.foo.service.RentalService"  
  4.     cache-home="true"  
  5.     lookup-home-on-startup="true"  
  6.     resource-ref="true">  
<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/>
<jee:remote-slsb/>配置一个远程EJB无状态会话BEAN
使用之前
Xml代码 复制代码  收藏代码
  1. <bean id="complexRemoteEjb"  
  2.       class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">  
  3.   <property name="jndiName" value="ejb/MyRemoteBean"/>  
  4.   <property name="businessInterface" value="com.foo.service.RentalService"/>  
  5.   <property name="cacheHome" value="true"/>  
  6.   <property name="lookupHomeOnStartup" value="true"/>  
  7.   <property name="resourceRef" value="true"/>  
  8.   <property name="homeInterface" value="com.foo.service.RentalService"/>  
  9.   <property name="refreshHomeOnConnectFailure" value="true"/>  
  10. </bean>  
<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>

基于schema配置
Xml代码 复制代码  收藏代码
  1. <jee:remote-slsb id="complexRemoteEjb"  
  2.     jndi-name="ejb/MyRemoteBean"  
  3.     business-interface="com.foo.service.RentalService"  
  4.     cache-home="true"  
  5.     lookup-home-on-startup="true"  
  6.     resource-ref="true"  
  7.     home-interface="com.foo.service.RentalService"  
  8.     refresh-home-on-connect-failure="true">  
<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">


lang schema
lang标签用来暴露用脚本语言写的bean,比如Jruby,Groovy,Beanshell
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:lang="http://www.springframework.org/schema/lang"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10. </beans>  
<?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.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">

<!-- bean definitions here -->
</beans>


jms schema
处理JMS相关的BEAN
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:jms="http://www.springframework.org/schema/jms"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10.   
  11. </beans>  
<?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.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">

<!-- bean definitions here -->

</beans>


tx (transaction) schema
事务处理
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="   
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd   
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">  
  10.   
  11. <!-- bean definitions here -->  
  12.   
  13. </beans>  
<?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.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">

<!-- bean definitions here -->

</beans>


aop schema
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10.   
  11. </beans>  
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- bean definitions here -->

</beans>



context schema
spring 2.5之后才有
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10.   
  11. </beans>  
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- bean definitions here -->

</beans>


<property-placeholder/>

根据指定资源文件(spring resource)激活${},用 PropertyPlaceholderConfigurer实现

<annotation-config/>
激活侦测配置bean上的注解
@Required ,@Autowired,@PreDestroy , @Resource , @PersistenceContext,@PersistenceUnit

<component-scan/> 自动侦测类,注解
<load-time-weaver/> 使用AspectJ
<spring-configured/> 使用AspectJ用在领域对象
<mbean-export/>导出Mbean

jdbc schema
配置内嵌数据库和初始化内嵌数据库
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  5.        xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">  
  8.   
  9. <!-- bean definitions here -->  
  10. </beans>  
<?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.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

<!-- bean definitions here -->
</beans>

<jdbc:embedded-database/>内嵌数据库支持 HSQL, H2,  Derby
Xml代码 复制代码  收藏代码
  1. <jdbc:embedded-database id="dataSource">  
  2.         <jdbc:script location="classpath:schema.sql"/>  
  3.         <jdbc:script location="classpath:test-data.sql"/>  
  4.     </jdbc:embedded-database>  
<jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:schema.sql"/>
        <jdbc:script location="classpath:test-data.sql"/>
    </jdbc:embedded-database>

<jdbc:initialize-database />
初始化内嵌数据库
Xml代码 复制代码  收藏代码
  1. <jdbc:initialize-database data-source="dataSource">  
  2.   <jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>  
  3.   <jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>  
  4. </jdbc:initialize-database>  
<jdbc:initialize-database data-source="dataSource">
  <jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>
  <jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>
</jdbc:initialize-database>


自定义扩展标签
  1. 编写schema
  2. 实现解析标签的NamespaceHandler
  3. 实现BeanDefinitionParser
  4. 钩入以上实现配置到Spring中

实现bean定义如下
Xml代码 复制代码  收藏代码
  1. <myns:dateformat id="dateFormat"  
  2.     pattern="yyyy-MM-dd HH:mm"  
  3.     lenient="true"/>  
<myns:dateformat id="dateFormat"
    pattern="yyyy-MM-dd HH:mm"
    lenient="true"/>


编写schema
Xml代码 复制代码  收藏代码
  1. <!-- myns.xsd (inside package org/springframework/samples/xml) -->  
  2.   
  3. <?xml version="1.0" encoding="UTF-8"?>  
  4. <xsd:schema xmlns="http://www.mycompany.com/schema/myns"  
  5.     xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  6.     xmlns:beans="http://www.springframework.org/schema/beans"  
  7.     targetNamespace="http://www.mycompany.com/schema/myns"  
  8.     elementFormDefault="qualified"  
  9.     attributeFormDefault="unqualified">  
  10.   
  11.    <xsd:import namespace="http://www.springframework.org/schema/beans"/>  
  12.   
  13.    <xsd:element name="dateformat">  
  14.       <xsd:complexType>  
  15.          <xsd:complexContent>  
  16.             <xsd:extension base="beans:identifiedType">  
  17.                <xsd:attribute name="lenient" type="xsd:boolean"/>  
  18.                <xsd:attribute name="pattern" type="xsd:string" use="required"/>  
  19.             </xsd:extension>  
  20.          </xsd:complexContent>  
  21.       </xsd:complexType>  
  22.    </xsd:element>  
  23.   
  24. </xsd:schema>  
<!-- myns.xsd (inside package org/springframework/samples/xml) -->

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.mycompany.com/schema/myns"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:beans="http://www.springframework.org/schema/beans"
    targetNamespace="http://www.mycompany.com/schema/myns"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

   <xsd:import namespace="http://www.springframework.org/schema/beans"/>

   <xsd:element name="dateformat">
      <xsd:complexType>
         <xsd:complexContent>
            <xsd:extension base="beans:identifiedType">
               <xsd:attribute name="lenient" type="xsd:boolean"/>
               <xsd:attribute name="pattern" type="xsd:string" use="required"/>
            </xsd:extension>
         </xsd:complexContent>
      </xsd:complexType>
   </xsd:element>

</xsd:schema>


2.编写NamespaceHandler
NamespaceHandler 接口有三个方法
init():第一次被使用时调用,注册BeanDefinitionParser
BeanDefinition parse(Element, ParserContext):作为顶级元素时被调用
BeanDefinitionHolder decorate(Node, BeanDefinitionHolder, ParserContext) :作为内嵌bean或不同namespace时被调用

Java代码 复制代码  收藏代码
  1. package org.springframework.samples.xml;   
  2.   
  3. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;   
  4.   
  5. public class MyNamespaceHandler extends NamespaceHandlerSupport {   
  6.   
  7.     public void init() {   
  8.         registerBeanDefinitionParser("dateformat"new SimpleDateFormatBeanDefinitionParser());   
  9.     }   
  10. }  
package org.springframework.samples.xml;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class MyNamespaceHandler extends NamespaceHandlerSupport {

    public void init() {
        registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());
    }
}


3.编写BeanDefinitionParser
Java代码 复制代码  收藏代码
  1. package org.springframework.samples.xml;   
  2.   
  3. import org.springframework.beans.factory.support.BeanDefinitionBuilder;   
  4. import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;   
  5. import org.springframework.util.StringUtils;   
  6. import org.w3c.dom.Element;   
  7.   
  8. import java.text.SimpleDateFormat;   
  9.   
  10. public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {    
  11.   
  12.    protected Class getBeanClass(Element element) {   
  13.       return SimpleDateFormat.class;    
  14.    }   
  15.   
  16.    protected void doParse(Element element, BeanDefinitionBuilder bean) {   
  17.       // this will never be null since the schema explicitly requires that a value be supplied   
  18.       String pattern = element.getAttribute("pattern");   
  19.       bean.addConstructorArg(pattern);   
  20.   
  21.       // this however is an optional property   
  22.       String lenient = element.getAttribute("lenient");   
  23.       if (StringUtils.hasText(lenient)) {   
  24.          bean.addPropertyValue("lenient", Boolean.valueOf(lenient));   
  25.       }   
  26.    }   
  27. }  
package org.springframework.samples.xml;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

import java.text.SimpleDateFormat;

public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 

   protected Class getBeanClass(Element element) {
      return SimpleDateFormat.class; 
   }

   protected void doParse(Element element, BeanDefinitionBuilder bean) {
      // this will never be null since the schema explicitly requires that a value be supplied
      String pattern = element.getAttribute("pattern");
      bean.addConstructorArg(pattern);

      // this however is an optional property
      String lenient = element.getAttribute("lenient");
      if (StringUtils.hasText(lenient)) {
         bean.addPropertyValue("lenient", Boolean.valueOf(lenient));
      }
   }
}


4.钩入配置  
   META-INF/spring.handlers指定命名空间处理器
  
Xml代码 复制代码  收藏代码
  1. http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler  
http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler

   META-INF/spring.schemas指定schema路径,用于schema验证(xsd放在class路径下)
  
Xml代码 复制代码  收藏代码
  1. http\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd  
http\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd


5.使用扩展标签

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       xmlns:myns="http://www.mycompany.com/schema/myns"  
  5.       xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns.xsd">  
  8.   
  9.    <!-- as a top-level bean -->  
  10.    <myns:dateformat id="defaultDateFormat" pattern="yyyy-MM-dd HH:mm" lenient="true"/>  
  11.   
  12.    <bean id="jobDetailTemplate" abstract="true">  
  13.       <property name="dateFormat">  
  14.          <!-- as an inner bean -->  
  15.          <myns:dateformat pattern="HH:mm MM-dd-yyyy"/>  
  16.       </property>  
  17.    </bean>  
  18.   
  19. </beans>  

你可能感兴趣的:(schema)