The injection of the spring-dependent java

A .DI: Dependency injection; DI

Inversion of Control and Dependency Injection are different versions of the same idea.

Create an object depends on the container. Object properties is provided by the container is set.

Assignment process object properties is called implantation.

Two .Spring how to inject attributes :

1. Common Property (String type and basic data), can be provided directly by the property

<bean id="user" class="cn.sxt.vo.User">
        <property name="name" value="张三疯"/>
        <property name="age" value="22"/>
    </bean>

2. Set the array

<property name="hobbies">
            <array>
                <value>足球</value>
                <value>蓝球</value>
                <value>乒乓球</value>
            </array>
        </property>

3.List settings and the same array

<property name="addreess">
            <list>
                <value>北京昌平</value>
                <value>山西平遥</value>
                <value>xxxx</value>
            </list>
        </property>

or

<property name="addreess">
            <array>
                <value>北京昌平</value>
                <value>山西平遥</value>
                <value>xxxx</value>
            </array>
        </property>

4. set collection set

<property name="books">
            <set>
                <value>大话设计模式</value>
                <value>head.first java</value>
            </set>
        </property>

5.Map collection set

<property name="cards">
            <map>
                <entry key="农业银行">
                    <value>ABC</value>
                </entry>
                <entry key="工商银行" value="ICBC"/>
            </map>
        </property>

6. Properties injection

<property name="appearance">
            <props>
                <prop key="weight">60kg</prop>
                <prop key="height">170cm</prop>
            </props>
        </property>

7. injected object

<! -  
            injection object   ref is a reference to another container object identifier
          -> 
        <Property name = "Role" ref = " the myRole " /> 
    </ the bean> 
    <the bean ID = " the myRole " class = " cn.sxt.vo.Role "> 
        <Property name =" ID "value =" 1001 "/> 
        <Property name =" name "value =" administrator "/> 
    </ the bean>

8. p namespace injection

Need to import the header file

<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">

Configuration

<! - implanted p-property namespace nature properties , desirable to provide a method to set the properties , except the property of write attribute bean -> 
    <bean ID = "R1" class = "cn.sxt.vo.Role " P: ID =" 1007 " P: name =" VIP "> </ the bean>

9. c Namespace injection

Need to import the header file

<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"
    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">

Configuration

<-! C-constructor namespace injection is intrinsically participate constructor injection , requires providing with a constructor parameter -> 
    <the bean ID = "R2" class = "cn.sxt.vo.Role" C: ID = "1006" c: name = "Member"> </ bean>

10. Null injection

<bean id="r3" class="cn.sxt.vo.Role">
        <property name="id" value="10"/>
        <property name="name"><null/></property>
    </bean>

 

Summary: In the spring, the injection properties generally divided into two categories;

 1. constructor injection

 2. Set the injection method

Note that: when using the constructor injection, a need to provide a corresponding method of construction; set when using injection methods, a need to provide a corresponding set methods. 

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11247831.html