Talk about SpringUtil of Spring source code [appendix: XML syntax] [writing XSD of RPC practice case]

1, Background

In writing RPC framework, dubbo's idea is used for reference, and dubbo's implementation is based on Spring Schema. So you need to know how to extend Spring Schema based on Spring.

2, Revenue

You can better understand the schema syntax in Spring and write your own framework in the process of integrating Spring.

3, Principle

3.1. XML syntax

This section lists the XML schemas related to the Spring core container.

3.2 Schema tool

For example, the util tag handles common utility configuration problems, such as configuration sets, reference constants, and so on. To use the tags in util mode, you need to declare the following prologue protocol at the top of the spring XML configuration file:

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

</beans>

So now I want to imitate dubbo to formulate my own Spring Schema. How do I write XSD? Based on the above, the following contents can be written:
My project name is xcxyz Mini Dubbo, and then the XML Spring Schema is miniDubbo

<?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:miniDubbo="http://www.xcxyz-mini-dubbo.com/schema/remote-service"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.xcxyz-mini-dubbo.com/schema/remote-service http://www.xcxyz-mini-dubbo.com/schema/remote-service.xsd">
</beans>

3.2.1,util:constant/

Previous configuration

<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 springfactorybean to implement FieldRetrievingFactoryBean, and sets the value of the isolation property on the bean to Java sql. Connection. The value of the transaction \ u serializable constant. All this is fine, but a bit verbose and (unnecessarily) exposes Spring's internal pipelines to end users.
The following XML schema based version is more concise and clearly expresses the developer's intention ("inject this constant value") and reads better.

<bean id="..." class="...">
    <property name="isolation">
        <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
    </property>
</bean>

Set bean properties or constructor parameters from field values
FieldRetrievingFactoryBean is a FactoryBean that retrieves static or non static field values. It is usually used to retrieve public static final constants, which can then be used to set the property value or constructor arg for another bean.
The following example demonstrates how to expose a static field 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 convenient usage form in which 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 about what the bean id is (so any other bean that references it must also use this longer name), but this form of definition is very concise and easy to use as an internal bean, because it is not necessary to specify an id 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>

How to inject

package javax.persistence;

public enum PersistenceContextType {

    TRANSACTION,
    EXTENDED
}

Set values for attributes

package example;

public class Client {

    private PersistenceContextType persistenceContextType;

    public void setPersistenceContextType(PersistenceContextType type) {
        this.persistenceContextType = type;
    }
}

Define entity bean s

<bean class="example.Client">
    <property name="persistenceContextType" value="TRANSACTION"/>
</bean>

3.2.2,util:property-path/

3.2.3,util:properties/

3.2.4,util:list/

3.2.5,util:map/

3.2.6,util:set/

3.3,AOP Schema

3.4,Context Schema

3.5,Beans Schema

4, Information

Spring's XML syntax official website

Keywords: Spring Spring Boot bean

Added by saandel on Tue, 01 Feb 2022 18:50:48 +0200