P namespace injection

P namespace injection

P namespace injection also uses the set method. The official purpose is to simplify the writing of the property tag of set injection

  • bean object
public class ConstructorDIDaoImpl implements ICustomerDao {

    private int id;
    private String name;
    private int age;
    private Date birthday;

    get/set()......

}

step
1. Import p namespace

xmlns:p="http://www.springframework.org/schema/p"

2. Write the configuration and use the p: attribute to complete the injection
Value type

p: Property name = value

-Reference type

p: Property name - ref = name of reference object

  • configuration file
<! -- p namespace injection, note: p namespace injection is actually a call to set method injection
    Step 1: import the p namespace (Location:
    ..\spring-framework-4.3.8.RELEASE-dist\spring-framework-4.3.8.RELEASE\
    docs\spring-framework-reference\html\beans.html)
    Step 2 configuration:
        p: Property name: assign value to member variable of simple type
        p: Attribute name ref: assign value to member variable of other bean types
 -->
 <bean id="pDao" class="cn.dao.impl.SetDependencyDaoImpl"
    p:id="1" p:name="Hello" p:age="18" p:birthday-ref="now"
 ></bean>
 <bean id="now" class="java.util.Date"></bean>

Set attribute injection

As the name implies, it is to pass values to the set members in a class. It is also injected by the set method, except that the data types of variables are all sets. Let's talk about injecting array, List,Set,Map,Properties.

  • The data structure is the same, and the labels can be interchanged
    • List structure:
      array/list/set
    • Map structure:
      map/entry/props/prop

The specific code is as follows:

public class CollectionDIServiceImpl implements CustomerService {

    private String[] arr;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties prop;

    set/get().......
}
  • configuration file
<!-- Injection of set type data, Description:
 Details: the data structure is the same, and the tag can call
    1.list/array/set
    2.map/properties

-->
<bean id="collectionDao" class="cn.dao.impl.CollectionDependencyDaoImpl">
    <!-- array -->
    <property name="array">
        <!-- <array>
            <value>array1</value>
            <value>array2</value>
            <value>array3</value>
        </array> -->
        <list>
            <value>array1</value>
            <value>array2</value>
            <value>array3</value>
        </list>
    </property>

    <!-- list -->
    <property name="list">
        <!-- <list>
            <value>list1</value>
            <value>list2</value>
             <value>list3</value>
        </list> -->
        <array>
            <value>list1</value>
            <value>list2</value>
            <value>list3</value>
        </array>
    </property>

    <!-- set -->
    <property name="set">
        <set>
            <value>set1</value>
            <value>set2</value>
            <value>set3</value>
        </set>
    </property>

    <!-- map -->
    <property name="map">
        <!-- <map>
            <entry key="mapk1" value="mapv1"></entry>
            <entry key="mapk2" value="mapv2"></entry>
            <entry key="mapk3" value="mapv3"></entry>
        </map> -->
        <props>
            <prop key="mapk1">mapv1</prop>
            <prop key="mapk2">mapv2</prop>
            <prop key="mapk3">mapv3</prop>
        </props>
    </property>

    <!-- properties -->
    <property name="prop">
        <!-- <props>
            <prop key="propk1">propv1</prop>
            <prop key="propk2">propv2</prop>
            <prop key="propk3">propv3</prop>
        </props> -->
        <map>
            <entry key="propk1" value="propv1"></entry>
            <entry key="propk2" value="propv2"></entry>
            <entry key="propk3" value="propv3"></entry>
        </map>
    </property>
</bean>

Property injection in the way of spiel: the way provided after spring 3. X

Spiel: Spring expression language

  • Syntax: {spiel}
<bean id="pDao" class="cn.dao.impl.SetDependencyDaoImpl"
    p:id="1" p:name="Hello" p:age="18" p:birthday-ref="now"
 ></bean>

<!-- SpEL How to inject --> 
 <bean id="SpelDao" class="cn.dao.impl.SetDependencyDaoImpl">
    <!-- 
        //Assign other object properties to yourself
        #{other object name. Property name}
    -->
    <property name="id" value="#{pDao.id}"></property>
    <property name="name" value="#{pDao.name}"></property>
    <property name="age" value="#{pDao.age}"></property>
    <property name="birthday" ref="now"></property>
 </bean>

Tags: Spring Attribute Java

Posted on Thu, 19 Mar 2020 13:46:02 -0400 by Pjack125