Spring IOC (inversion control)

Inversion of Control:

  After using the Spring Framework object is not created by the caller, but created by the Spring container

  Spring container will be responsible for the relationship between the program control rather than code directly controlled by the caller program

  Such control is transferred from the application to the Spring container, control reversal took place, this is the Spring Inversion of Control

  Process to obtain a dependent object is reversed, after reversal control, the process of obtaining their dependent objects from the active management becomes injected from the IOC container

  Thus, "Inversion of Control," also called "DI" (Dependency Injection) short (DI) called dependency injection, that is, the IOC container during operation

  Some of the dynamic dependency injection into an object

  Therefore, dependency injection (DI) and reverse control (IOC) are described from different angles of the same thing, that refers to the introduction of IOC container

  Manner using dependency injection, decoupling between objects

 Example code:

  1, and the set of common data type injection

      [1] Create a user entity class provides methods and set construction and toString

      

 

      [2] write configuration file

         

    <bean id="user" class="com.bdqn.cn.pojo.User">
         <!-- 通过构造注入 -->
<!--        <constructor-arg index="0" value="张三"/>
        <constructor-arg index="1">
            <list>
                <value>list1</value>
                <value>list2</value>
            </list>
        </constructor-arg>
        <constructor-arg index="2">
            <map>
                <entry key="hhh" value="哈哈哈"/>
                <entry key="zzz" value="啧啧啧"/>
                <entry key="bbb" value="不不不"/>
            </map>
        </constructor-arg>
                <constructor-arg index="3">
            <set>
                
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
            
            </set>
        </constructor-arg>
        <constructor-arg index="4">
        
            <array>
                
                <value>aaaaaaaa</value>                
                <value>bbbbbbbb</value>                
            
            </array>
        
        </constructor-arg> -->
        
        
        
        
        <!-- 通过setter方法注入参数 -->
        <property name="username" value="赵云" />
        <property name="list">
        
            <list>
                
                <value>看书</value>
                <value>学习</value>
            
            </list>
        
        </property>
        <property name="map">
        
            <map>
                
                <entry key="guangshou" value="广州" />
                <entry key="shanghai" value="上海" />
                <entry key="shenzhen" value="深圳" />
            
            </map>
        
        </property>
        <property name="set">
        
            <set>
            
                <value>关羽</value>
                <value>张飞</value>
                <value>赵云</value>
                <value>马超</value>
                <value>黄忠</value>
            
            </set>        
        
        </property>
        <property name="array">
        
            <array>
                
                <value>aaaaaaaa</value>
                <value>bbbbbbbb</value>
            
            </array>
        
        </property>
    </bean>

      [3] write test classes

      

    2, target injection

     [1] create the interface and implementation class

      

 

 

     [2] create a business layer

 

      

 

 

      

public  class UserServiceImpl the implements UserService {
     // here embodied not only declared 
    Private UserDao userDao;
     // to set mode parameters passed userDao 
/ *     public void setUserDAO (UserDao userDao) { 
        this.userDao = userDao; 
    } * / 
    // constructor in the form of 
    public UserServiceImpl (UserDao userDao) {
         Super ();
         the this .userDao = userDao; 
    } 

    @Override 
    public  void say () {
         the this .userDao.say (); 
        System.out.println ("UserServiceImpl...");
    }


}

 

       【3】编写配置文件

      

 

 

 

 

 

       【4】编写测试类

      

 

 

 

 

 

 

 

 

 

 

 

 

      

Guess you like

Origin www.cnblogs.com/yz-bky/p/12576753.html