spring的IOC与DI

一、ioc与di的区别

DI 介绍 

Dependency Injection 依赖注入.需要有IOC 的环境,Spring 创建这个类的过程中,Spring 将类的依赖的属性设置进去.

IOC与DI的的区别: 

IOC:  控制反转,将类的对象的创建交给Spring类管理创建. 
DI:    依赖注入,将类里面的属性在创建类的过程中给属性赋值. 
DI和IOC的关系: DI不能单独存在,DI需要在IOC的基础上来完成.

这样做得好处:做到了单一职责,并且提高了复用性,解耦。

举例:IOC 

以下配置则是交给了spring容器创建Bean,以前没用spering时 则,User user=new User();

那么,如果要获取spring中的bean , 则

    ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
     User user=(User) ac.getBean("user");

<bean id="user" class="com.spring.domain.User">
        <property name="name">
            <value>张三</value>
        </property>
    </bean>

 bean标签都代表着需要被创建的对象并通过property标签可以为该类注入其他依赖对象;

DI 依赖注入:

如在service层中要引用Dao,

<!-- 声明accountDao对象,交给spring创建 -->
    <bean name="accountDao" class="com.zejian.spring.springIoc.dao.impl.AccountDaoImpl"/>
    <!-- 声明accountService对象,交给spring创建 -->
    <bean name="accountService" class="com.zejian.spring.springIoc.service.impl.AccountServiceImpl">
          <!-- 注入accountDao对象,需要set方法-->
          <property name="accountDao" ref="accountDao"/>
    </bean>
  <property name="accountDao" ref="accountDao"/>  name:service属性的名字,ref 引用的类

其实是当一个bean实例引用到了另外一个bean实例时spring容器帮助我们创建依赖bean实例并注入(传递)到另一个bean中,如上述案例中的AccountService依赖于AccountDao,Spring容器会在创建AccountService的实现类和AccountDao的实现类后,把AccountDao的实现类注入AccountService实例中,下面分别介绍setter注入和构造函数注入。

依赖注入,则被注入的属性需要有set方法;

除了上述的对象注入同时也可以注入简单值和map、set、list、数组,简单值注入使用<property>的value属性:

public class Account {
    private String name;
    private String pwd;
    private List<String> citys;
    private Set<String> friends;
    private Map<Integer,String> books;

    public void setName(String name) {
        this.name = name;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setCitys(List<String> citys) {
        this.citys = citys;
    }

    public void setFriends(Set<String> friends) {
        this.friends = friends;
    }

    public void setBooks(Map<Integer, String> books) {
        this.books = books;
    }
}

<!-- setter通过property 注入属性值,普通类型使用value -->
<bean id="account" scope="prototype" class="com.zejian.spring.springIoc.pojo.Account" >
    <property name="name" value="I am SpringIOC1" />
    <property name="pwd" value="123" />
    <!-- 注入map -->
    <property name="books">  
        <map>  
          <entry key="10" value="CoreJava">  
            </entry>  
            <entry key="11" value="JavaWeb">  
            </entry>  
            <entry key="12" value="SSH2">  
            </entry>  
        </map>  
  </property>  
  <!-- 注入set -->
  <property name="friends">  
       <set>  
           <value>张龙</value>  
           <value>老王</value>  
           <value>王五</value>  
       </set>  
   </property>  
  <!-- 注入list -->
   <property name="citys">  
       <list>  
           <value>北京</value>  
           <value>上海</value>  
           <value>深圳</value>
           <value>广州</value>  
       </list>  
   </property>  
</bean>

//构造函数注入
//构造注入也就是通过构造方法注入依赖,构造函数的参数一般情况下就是依赖项,spring容器会根据bean//中指定的构造函数参数来决定调用那个构造函数
public class AccountServiceImpl implements AccountService{
    /**
     * 需要注入的对象Dao层对象
     */
    private AccountDao accountDao;

    /**
     * 构造注入
     * @param accountDao
     */
    public AccountServiceImpl(AccountDao accountDao){
        this.accountDao=accountDao;
    }

    //........
}

//xml配置如下:
<bean name="accountDao" class="com.zejian.spring.springIoc.dao.impl.AccountDaoImpl"/>
<!-- 通过构造注入依赖 -->
<bean name="accountService" class="com.zejian.spring.springIoc.service.impl.AccountServiceImpl">
    <!-- 构造方法方式注入accountDao对象,-->
    <constructor-arg index=0  ref="accountDao"/>
</bean>
//构造对象方式和属性的方式依赖注入; 则都是通过ref绑定,只是一个是 onstructor-arg ;property, index 属性则表示该构造器中的第几个参数
原文地址:https://www.cnblogs.com/hellohero55/p/12642702.html