The second spring learning portal

IOC dependency injection

IOC: inverse of control: Inversion of Control

In 2004, Martin Fowler discusses the same problem, since the IOC is Inversion of Control, so in the end is, after detailed analysis and argument, he got the answer "what is inverted control of it?": "Get dependent object of the process is reversed. " After the control is reversed, the process of obtaining their dependent objects from the active management becomes injected from the IOC container. So he "inversion of control" took a more appropriate name is "dependency injection (Dependency Injection)". His answer, in fact, shows how to achieve the IOC: the injection. The so-called dependency injection, that is, the IOC container during operation, dynamic dependencies injected into certain objects.

 

Dependency Injection: spring managed bean, when it is dependent on a managed bean of the other spring time, spring is responsible for injecting it came

Injection method

1, constructor injection (the Constructor)

2, injection property (the setter)

3, interface injection (rarely used)

Case number one:

EmpDao Interface:

package com.complex;

public interface EmpDao {
    void insert();
}

EmpDaoImpl implementation class:

package com.complex;

public class EmpDaoImpl implements EmpDao{

    @Override
    public void insert() {
        System.out.println("this is EmpDao Method insert");
    }
}

EmpService Interface:

package com.complex;

public interface EmpService {
    void insert();
}

EmpServiceImpl implementation class:

package com.complex;

public class EmpServiceImpl implements EmpService{
    private EmpDao empDao;

    public EmpServiceImpl(){}

    // constructor injection 
    public EmpServiceImpl (EmpDao empDao) { the this .empDao = empDao;}

    @Override
    public void insert() {
        empDao.insert();
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="empDaoImpl" class="com.complex.EmpDaoImpl"></bean>

    < Bean the above mentioned id = "empServiceImpl" class = "com.complex.EmpServiceImpl" > 
        <-! Can see us through constructor injection, to achieve the instantiated bean when the bean empDaoImpl will automatically injected into this bean empServiceImpl examples of the constructor of the bean -> 
        < constructor-Arg REF = "empDaoImpl" > </ constructor-Arg > 
    </ the bean > 
</ Beans >

Main test:

package com.complex;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_complex.xml");
        EmpServiceImpl empServiceImpl = context.getBean("empServiceImpl", EmpServiceImpl.class);
        empServiceImpl.insert();
    }

}

Test results: this is EmpDao Method insert

We realized injected through the constructor when instantiating Bean's EmpDao via dependency injection

Why EmpServiceImpl put EmpDao interface type, because it follows the Dependency Inversion principle: the abstract should not depend on the details, the details should depend on the abstract.

 

Detailed Dependency Injection

1, how a plurality of attributes constructor injection

BookInfo class

package com.basic;

import java.math.BigDecimal;

public class BookInfo {
    private int id;
    private String name;
    private BigDecimal price;
    private int count;

    public BookInfo(int id, String name, BigDecimal price, int count) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.count = count;
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bookInfo" class="com.basic.BookInfo">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="童话故事"></constructor-arg>
        <constructor-arg name="price" value="15"></constructor-arg>
        <constructor-arg name="count" value="100"></constructor-arg>
    </bean>
</beans>
Injected through the constructor We can specify the configuration parameters, how spring is know to pass the parameters which it?

constructor-arg provided us with three parameters specified method name

name: set the parameters name

index: Set Parameters Index

type: Parameter Type

If not set, then, spring will go in accordance with the order parameter passing one by one.

2, how to attribute the injection?

BookInfo class

package com.basic;

import java.math.BigDecimal;

public class BookInfo {
    private int id;
    private String name;
    private BigDecimal price;
    private int count;

    // GET and set methods are omitted here 
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bookInfo" class="com.basic.BookInfo">
        <property name="id" value="1"></property>
        <= "fairy tale"value= "name"nameProperty></property>
        <property name="price" value="15"></property>
        <property name="count" value="100"></property>
    </bean>
</beans>

Injection properties achieved by setting property, name corresponding to the specified attribute.

3, how complex type injection?

Earlier we achieve is a simple type, then the question is, how complex type injection? For example list, map, etc. entity class

ShopCar

package com.basic;

import java.util.List;
import java.util.Map;
import java.util.Set;

public  class ShopCar {
     Private String sname;    // basic types 
    Private List <the BookInfo> List;     // List set 
    Private the Map <Integer, the BookInfo> Map;   // Map set 
    Private the Set <the BookInfo> bookInfoSet;   // SET set 
    Private the BookInfo bookInfo ;   // entity object

    public ShopCar(String sname, List<BookInfo> list, Map<Integer, BookInfo> map, Set<BookInfo> bookInfoSet, BookInfo bookInfo) {
        this.sname = sname;
        this.list = list;
        this.map = map;
        this.bookInfoSet = bookInfoSet;
        this.bookInfo = bookInfo;
    }
    
    // the SET and get way too long omitted here .... 
}

 

Let's look at a complex type constructor injection

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bookInfo" class="com.basic.BookInfo">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="童话故事"></constructor-arg>
        <constructor-arg name="price" value="15"></constructor-arg>
        <constructor-arg name="count" value="100"></constructor-arg>
    </bean>

    < Bean the above mentioned id = "shopCar" class = "com.basic.ShopCar" > 
<-!         Common types with the same value -> 
        < constructor-Arg name = "sname" value = "cart 1" > </ constructor- Arg > 
<-!         entity class with REF -> 
        < constructor Arg- name = "bookInfo" REF = "bookInfo" > </ constructor-Arg > 
<-!         List type setting -> 
        < constructor Arg- name = "List" >
            <list>
                <ref bean="bookInfo"></ref>
                <bean class="com.basic.BookInfo">
                    <property name="id" value="2"></property>
                    <property name="name" value="小红帽"></property>
                    <property name="price" value="15"></property>
                    <property name="count" value="100"></property>
                </bean>
            </list>
        </constructor-arg>
<!--        map设置-->
        <constructor-arg name="map">
            <map>
                <entry key="1" value-ref="bookInfo"></entry>
            </map>
        </constructor-arg>
<!--        set设置-->
        <constructor-arg name="bookInfoSet">
            <set>
                <ref bean="bookInfo"></ref>
            </set>
        </constructor-arg>
    </bean>
</beans>

The complex property injection

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bookInfo" class="com.basic.BookInfo">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="童话故事"></constructor-arg>
        <constructor-arg name="price" value="15"></constructor-arg>
        <constructor-arg name="count" value="100"></constructor-arg>
    </bean>

    <bean id="shopCar" class="com.basic.ShopCar">
        <!--        基本类型-->
        <property name="sname" value="shop2"></property>
<!--        实体类型-->
        <property name="bookInfo" ref="bookInfo"></property>
<!--        list类型-->
        <property name="list">
            <list>
                <ref bean="bookInfo"></ref>
            </list>
        </property>
<!--        map类型-->
        <property name="map">
            <map>
                <entry key="1" value-ref="bookInfo"></entry>
            </map>
        </property>
<!--        set类型-->
        <property name="bookInfoSet">
            <set>
                <ref bean="bookInfo"></ref>
            </set>
        </property>
    </bean>
</beans>

End!

 

 

Guess you like

Origin www.cnblogs.com/liweixml/p/11697527.html