Spring配置中的c标签和p标签

XML shortcut with the p-namespace

The p-namespace enables you to use the bean element’s attributes, instead of nested <property/> elements, to describe your property values and/or collaborating beans.

Spring supports extensible configuration formats with namespaces, which are based on an XML Schema definition. The beans configuration format discussed in this chapter is defined in an XML Schema document. However, the p-namespace is not defined in an XSD file and exists only in the core of Spring.

The following example shows two XML snippets that resolve to the same result: The first uses standard XML format and the second uses the p-namespace.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="foo@bar.com"/>
    </bean>

<bean name="p-namespace" class="com.example.ExampleBean"
        p:email="foo@bar.com"/>
</beans>

 

The example shows an attribute in the p-namespace called email in the bean definition. This tells Spring to include a property declaration. As previously mentioned, the p-namespace does not have a schema definition, so you can set the name of the attribute to the property name.

This next example includes two more bean definitions that both have a reference to another bean:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="john-classic" class="com.example.Person">
        <property name="name" value="John Doe"/>
        <property name="spouse" ref="jane"/>
    </bean>

<bean name="john-modern"
        class="com.example.Person"
        p:name="John Doe"
        p:spouse-ref="jane"/>

<bean name="jane" class="com.example.Person">
        <property name="name" value="Jane Doe"/>
    </bean>
</beans>

 

As you can see, this example includes not only a property value using the p-namespace, but also uses a special format to declare property references. Whereas the first bean definition uses <property name=”spouse” ref=”jane”/> to create a reference from bean john to bean jane, the second bean definition uses p:spouse-ref=”jane” as an attribute to do the exact same thing. In this case spouse is the property name, whereas the -ref part indicates that this is not a straight value but rather a reference to another bean.

The p-namespace is not as flexible as the standard XML format. For example, the format for declaring property references clashes with properties that end in Ref, whereas the standard XML format does not. We recommend that you choose your approach carefully and communicate this to your team members, to avoid producing XML documents that use all three approaches at the same time.

 

 

XML shortcut with the c-namespace

Similar to the the section called “XML shortcut with the p-namespace”, the c-namespace, newly introduced in Spring 3.1, allows usage of inlined attributes for configuring the constructor arguments rather then nested constructor-arg elements.

Let’s review the examples from the section called “Constructor-based dependency injection” with the c: namespace:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="bar" class="x.y.Bar"/>
    <bean id="baz" class="x.y.Baz"/>

<!-- traditional declaration -->
    <bean id="foo" class="x.y.Foo">
        <constructor-arg ref="bar"/>
        <constructor-arg ref="baz"/>
        <constructor-arg value="foo@bar.com"/>
    </bean>

<!-- c-namespace declaration -->
    <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com"/>

</beans>

 

The c: namespace uses the same conventions as the p: one (trailing -ref for bean references) for setting the constructor arguments by their names. And just as well, it needs to be declared even though it is not defined in an XSD schema (but it exists inside the Spring core).

For the rare cases where the constructor argument names are not available (usually if the bytecode was compiled without debugging information), one can use fallback to the argument indexes:

<!-- c-namespace index declaration -->
<bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz"/>
Due to the XML grammar, the index notation requires the presence of the leading _ as XML attribute names cannot start with a number (even though some IDE allow it).

 

应用示例

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.2.xsd">
<beanid="seekManager"class="com.xxx.openframework.outbound.SeekManager"
p:dynamicSeek="${seekManager.dynamicSeek}"p:startUpSeek="${seekManager.onStartUpSeek}"/>

<!--对外呼出服务注册组件-->
<beanid="outboundServiceRegistry"
class="com.xxx.openframework.outbound.registry.DefaultOutboundServiceRegistry"/>



<!--对外呼出服务注册解析主控-->
<beanid="outboundServiceRegistryConfigParser"
class="com.xxx.openframework.outbound.config.XmlOutboundServiceRegistryConfigParser"
c:_0-ref="outboundServiceRegistry"c:_1-ref="handlerStackRegistry"
p:seekManager-ref="seekManager"p:loadBalancerBean-ref="loadBalancerBean">
<propertyname="endpointConfigParserRegistry">
<map>
<entrykey="http">
<bean
class="com.xxx.openframework.outbound.config.http.HttpEndpointConfigParser"/>
</entry>
<entrykey="wtc">
<bean
class="com.xxx.openframework.outbound.config.tuxedo.WtcEndpointConfigParser"/>
</entry>
</map>
</property>
<propertyname="configrations">
<list>
<value>classpath*:framework/outbound/*.xml</value>
<value>${outboundServiceRegistryConfigParser.configrations.instemployee}</value>
<value>${outboundServiceRegistryConfigParser.configrations}</value>
<value>${jgyg.outboundServiceRegistryConfigParser.configrations}</value>
</list>
</property>
</bean>

<!--对外呼出服务主控-->
<beanid="outboundServiceExecutor"
class="com.xxx.openframework.outbound.DefaultOutboundServiceExecutor"
c:_0-ref="outboundServiceRegistry">
<constructor-arg>
<map>
<entrykey="http">
<bean
class="com.xxx.openframework.outbound.DefaultOutboundServiceInvoker">
<constructor-arg>
<!--Http呼出通讯适配器-->
<bean
class="com.xxx.openframework.communi.client.http.HttpClientAdapter"
p:logEnabled="${clientAdapter.logEnabled}"p:logLevel="${clientAdapter.logLevel}"
p:seekManager-ref="seekManager"p:defaultFileTransPort="${requestWithFile.defaultPort}"
p:jfServerVersion="${jfServer.version}"
p:loadBalancerBean-ref="loadBalancerBean"/>
</constructor-arg>
</bean>
</entry>
</map>
</constructor-arg>
</bean>

<!--对外呼出安全报文头处理器-->
<beanid="outboundSecurityHeaderHandler"
class="com.xxx.openframework.handler.support.outbound.OutboundSecurityHeaderHandler"
p:secNodeId="#{securityManager.secNodeId}"p:mode="#{securityManager.mode}"
p:logEnabled="${securityHandler.logEnabled}"p:logLevel="${securityHandler.logLevel}"
p:maxMessageLength="${outboundServiceRequest.maxMessageLength}"
p:tracemode="${trace.mode}"/>

<!--对外呼出数据转换处理器-->
<beanid="outboundDataTransformHandler"
class="com.xxx.openframework.handler.support.outbound.OutboundDataTransformHandler"
p:mode="${trace.mode}"c:_0-ref="dataTransformManager"/>

<!--对外呼出报文头处理器-->
<beanid="outboundRequestHeaderHandler"
class="com.xxx.openframework.handler.support.outbound.OutboundRequestHeaderHandler"/>

<!--对外呼出响应带附件处理器addedbyyangtao3.zh20140317-->
<beanid="outboundFileTransferHandler"
class="com.xxx.openframework.handler.support.outbound.OutboundFileTransferHandler"
p:seekManager-ref="seekManager"p:mode="${trace.mode}"p:rspFileEnabled="${responseWithFile.enabled}"
p:defaultFileTransPort="${responseWithFile.defaultPort}"p:defaultFileDeposit="${responseWithFile.location}"
p:jfServerVersion="${jfServer.version}"p:dirWithDate="${responseWithFile.dirWithDate}"
p:loadBalancerBean-ref="loadBalancerBean"/>

<!--P4负载均衡包装类-->
<beanid="loadBalancerBean"
class="com.xxx.openframework.loadbalancer.registry.LoadBalancerBean"/>

</beans>

 

留下评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

%d 博主赞过: