How to simplify the Bean configuration of Spring

Posted by ruttiger on Sat, 21 Mar 2020 18:00:34 +0100

1 simplified configuration

Spring provides a simplified configuration for literals, reference beans, and collections, so if you don't use the special features in the complete configuration, it is recommended to try to use a simplified configuration.

1.1 face value

1.1.1 literal attribute

Before simplification:

<property name="name">
    <value>Brilliant Qianyang</value>
</property>

After simplification:

 <property name="name" value="Brilliant Qianyang"/>

1.1.2 constructor parameters

Before simplification:

<constructor-arg type="java.lang.String">
    <value>Brilliant Qianyang</value>
</constructor-arg>

After simplification:

<constructor-arg type="java.lang.String" value="Brilliant Qianyang"/>

1.1.3 set elements

Before simplification:

<property name="income">
    <map>
        <entry>
            <key>
                <value>first quarter</value>
            </key>
            <value>20000.00</value>
        </entry>
    </map>
</property>

After simplification:

<property name="income">
    <map>
        <entry key="first quarter" value="20000.00"/>
    </map>
</property>

Note: XML special processing tags <! [CDATA []] > cannot be used if simplified, but we can use XML escape sequences to process these special characters.

1.2 reference object properties

1.2.1 literal attribute

Before simplification:

<property name="author">
    <ref bean="author"></ref>
</property>

After simplification:

 <property name="author" ref="author"/>

1.2.2 constructor parameters

Before simplification:

<constructor-arg>
    <ref bean="author"/>
</constructor-arg>

After simplification:

<constructor-arg ref="author"/>

1.2.3 set elements

Before simplification:

<property name="income">
    <map>
        <entry>
            <key>
                <ref bean="keyBean"/>
            </key>
            <ref bean="valueBean/>
        </entry>
    </map>
</property>

After simplification:

<property name="income">
    <map>
        <entry key-ref="keyBean" value-ref="valueBean"/>
    </map>
</property>

The simplified form of < ref > is < ref bean = "XXX" >.

2 p namespace

Spring 2.5 + introduces the p namespace, which can further simplify the configuration of XML.

<?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: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 id="author" class="net.deniro.spring4.bean.Author"/>
    <bean id="book" class="net.deniro.spring4.bean.Book"
          p:name="veil" p:author-ref="author">
    </bean>
</beans>

First declare the p namespace, and then configure the property values of the Bean.

The property configuration format of a literal is:

p: < property name > = "XXX"

The property configuration format of a literal is:

p: < property name > - ref = "XXX"

Because the property name in the p namespace is variable, you do not need to specify the schema definition file in xsi:schemaLocation.

Topics: xml Spring Attribute Java