Talk about Spring's various types of dependency injection parameters

1 Contains XML special symbols

In a Spring configuration file, you can use literals to provide configuration values. If the configured value contains XML special symbols, you can add an XML special handling tag to the attribute value <![CDATA[]]>to make the XML parser treat the string in the tag as normal text.

There are 5 special characters in XML, and there are two ways to treat these characters specially.

  • Use tags <![CDATA[]]>to wrap special characters.
  • Use XML escape sequences to represent these characters.
Special characters escape sequence
< &lt;
> &gt;
& &amp;
" &quot;
' &apos;

Note: Spring does not ignore leading and trailing whitespace in tag content strings.

2 Reference to other beans

Beans defined in the Spring IoC container can reference each other.

<bean id="author" class="net.deniro.spring4.bean.Author">
    <property name="name" value="deniro"/>
</bean>
<bean id="book" class="net.deniro.spring4.bean.Book">
    <property name="author">
        <ref bean="author"></ref>
    </property>
</bean>

<ref>Elements can reference other beans in the container through the following 3 properties:

Attributes illustrate
bean Beans in the same container or parent container can be referenced.
local Only beans defined in the same configuration file can be referenced, and the validity will be checked automatically.
parent Refer to the Bean in the parent container, for example <ref parent="xxx">, indicating that the parent container of this Bean is xxx.

Father container arrangement:

<!-- 父容器-->
<bean id="author" class="net.deniro.spring4.bean.Author">
    <property name="name" value="deniroFather"/>
</bean>

Child container arrangement:

<bean id="author" class="net.deniro.spring4.bean.Author">
    <property name="name" value="deniro"/>
</bean>

<bean id="book" class="net.deniro.spring4.bean.Book">
    <property name="author">
        <ref parent="author"/>
    </property>
</bean>

The same bean id as the parent container exists in the child container configuration (author here), in the book, we are using <ref parent="author"/>it, so it refers to the bean of the parent container.

ApplicationContext parent = new ClassPathXmlApplicationContext
        (new String[]{"parent.xml"});
ApplicationContext context = new ClassPathXmlApplicationContext(new
        String[]{"beans.xml"},
        parent);////把父容器上下文作为参数传入

3 Internal Beans

If a bean is only referenced within a certain bean, it can be referenced as an inner bean:

<bean id="book" class="net.deniro.spring4.bean.Book">
    <property name="author">
        <bean class="net.deniro.spring4.bean.Author">
            <property name="name" value="deniro"/>
        </bean>
    </property>
</bean>

An inner bean is similar to Java's anonymous inner class, it has no name, so the instance can only be injected at the point of declaration.

Note: The configuration of the id, name and scope properties of the internal bean is invalid. The scope property defaults to prototype and cannot be changed.

4 null values

Sometimes you need to inject a null value, then you can use <null/>:

<bean id="book2" class="net.deniro.spring4.bean.Book">
    <property name="name"><null/></property>
</bean>

5 Cascading properties

Spring supports cascading properties. Suppose we want to inject the author's name directly into the book:

<bean id="book3" class="net.deniro.spring4.bean.Book">
    <property name="author.name" value="deniro"/>
</bean>

You also need to initialize the author property of the Book class:

private Author author=new Author();

If the author property is not initialized, Spring will throw a NullValueInNestedPathException exception.

Cascading properties at any level can be configured as long as the configured bean has a class structure corresponding to the cascading properties.

6 Collection types

Spring provides dedicated configuration tags for List, Set, Map and Properties.

6.1 List

<bean id="book4" class="net.deniro.spring4.bean.Book">
       <property name="labels">
           <list>
               <value>历史</value>
               <value>传记</value>
           </list>
       </property>
    </bean>

Add the labels property to Book:

/**
 * 标签
 */
private List<String> labels=new ArrayList<String>();

public List<String> getLabels() {
    return labels;
}

public void setLabels(List<String> labels) {
    this.labels = labels;
}

The list property can either be <value>injected with string or <ref>with other beans in the container.

Note: If an attribute type can be configured by the literal value of a string, then the corresponding array type (such as: String[], int[]) can also be configured <list>in the way of .

6.2 Set

The Set configuration is similar to the List configuration:

<bean id="book5" class="net.deniro.spring4.bean.Book">
    <property name="labelSet">
        <set>
            <value>历史</value>
            <value>传记</value>
        </set>
    </property>
</bean>

labelSet must be of type Set in Book:

private Set<String> labelSet=new HashSet<String>();

public Set<String> getLabelSet() {
    return labelSet;
}

public void setLabelSet(Set<String> labelSet) {
    this.labelSet = labelSet;
}

6.3 Map

We add an imprint for Book:

/**
 * 版本
 */
private Map<String,String> versions=new HashMap();

public Map<String, String> getVersions() {
    return versions;
}

public void setVersions(Map<String, String> versions) {
    this.versions = versions;
}

Configuration:

<bean id="book6" class="net.deniro.spring4.bean.Book">
    <property name="versions">
        <map>
            <entry>
                <key><value>1</value></key>
                <value>2017-1</value>
            </entry>
            <entry>
                <key><value>2</value></key>
                <value>2017-2</value>
            </entry>
        </map>
    </property>
</bean>

If the key and value of a Map element are objects, it can be configured like this:

<entry>
    <key><ref bean="keyBean"/></key>
    <ref bean="valueBean"/>
</entry>

6.4 Properties

The key and value of the Map element can be any type of object, while the key and value of the Properties element can only be strings, so the Properties element is actually a special case of the Map element.

We add an address attribute column for Author:

/**
 * 地址
 */
private Properties addresses=new Properties();

public Properties getAddresses() {
    return addresses;
}

public void setAddresses(Properties addresses) {
    this.addresses = addresses;
}

Configuration:

<bean id="author1" class="net.deniro.spring4.bean.Author">
    <property name="addresses">
        <props>
            <prop key="home">北京</prop>
            <prop key="work">杭州</prop>
        </props>
    </property>
</bean>

Because the key value of the Properties element can only be a string, the configuration is simpler than Map.

6.5 Strongly Typed Collections

Strong type refers to the wrapper type. If there is a strongly typed collection in the POJO, Spring can also inject:

/**
 * 收入
 */
private Map<String,Double> income=new HashMap<String, Double>();

public Map<String, Double> getIncome() {
    return income;
}

public void setIncome(Map<String, Double> income) {
    this.income = income;
}

Configuration:

<bean id="author2" class="net.deniro.spring4.bean.Author">
    <property name="income">
        <map>
            <entry>
                <key>
                    <value>第一季度</value>
                </key>
                <value>20000.00</value>
            </entry>
            <entry>
                <key>
                    <value>第二季度</value>
                </key>
                <value>30000.00</value>
            </entry>
        </map>
    </property>
</bean>

When the Spring container injects a strongly typed collection, it will determine the type of the element and convert the corresponding configuration value to the corresponding data type.

6.6 Merging collections

Spring allows children to <bean>inherit <bean>collection elements with properties of the same name in their parent.

<!-- 父 Bean -->
<bean id="fatherBook" class="net.deniro.spring4.bean.Book" abstract="true">
    <property name="labels">
        <list>
            <value>历史</value>
            <value>地理</value>
        </list>
    </property>
</bean>
<!-- 子 Bean -->
<bean id="childBook" class="net.deniro.spring4.bean.Book" parent="fatherBook">
    <property name="labels">
        <list merge="true">
            <value>天文</value>
            <value>数学</value>
        </list>
    </property>
</bean>

In the configuration, set the abstract attribute of the parent bean to true, indicating that it is an abstract bean; then specify the id of the parent bean in the parent attribute of the child bean; finally, set the merge of the collection tag to true, indicating that it is merged.

6.7 Beans of Collection Types

All of the above are to configure the properties of the collection type in a bean. If you need a collection-type bean, you need to use the util namespace. First introduce the util namespace in the Spring configuration file header:

<?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-4.0.xsd
       ">

Configuration List:

<util:list id="type" list-class="java.util.LinkedList">
    <value>人文</value>
    <value>科技</value>
</util:list>

Configure Set:

<util:set id="type2" set-class="java.util.HashSet">
    <value>人文</value>
    <value>科技</value>
</util:set>

Configure Map:

<util:map id="config" map-class="java.util.HashMap">
    <entry key="账号" value="admin"/>
    <entry key="密码" value="123456"/>
</util:map>

Tip:
* The xxx-class attribute is optional.
* <util:list>and <util:set>has the value-type attribute, which can specify the value type in the collection.
* <util:map>Has key-type and value-type attributes, you can specify the type of the key or value of the Map.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325846542&siteId=291194637