一:DI Dependency Injection ,依赖注入

is a :是一个,继承。

has a:有一个,成员变量,依赖。

class B {

           private A a;   //B类依赖A

        }

依赖:一个对象需要使用另一个对象

注入:通过setter方法进行另一个对象实例设置。

l 例如:

class BookServiceImpl{

        //之前开发:接口 = 实现类  (servicedao耦合)

//private BookDao bookDao = new BookDaoImpl();

  //spring之后 (解耦:service实现类使用dao接口,不知道具体的实现类)

private BookDao bookDao;

setter方法

   }

模拟spring执行过程

创建service实例:BookService bookService = new BookServiceImpl() -->IoC  <bean>

创建dao实例:BookDao bookDao = new BookDaoImple() -->IoC

dao设置给servicebookService.setBookDao(bookDao); -->DI   <property>

 

1.目标类

BookDao.java
package com.jd.di;

/**
 * @author weihu
 * @date 2018/8/8/008 23:44
 * @desc
 */
public interface BookDao {
    void save();
}
BookService.java
package com.jd.di;

/**
 * @author weihu
 * @date 2018/8/8/008 23:46
 * @desc BookService依赖BookDao
 */
public interface BookService {
    void addBook();
}
BookDaoImpl.java
package com.jd.di.impl;

import com.jd.di.BookDao;

/**
 * @author weihu
 * @date 2018/8/8/008 23:45
 * @desc
 */
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("di save book!");
    }
}
BookServiceImpl.java
package com.jd.di.impl;

import com.jd.di.BookDao;
import com.jd.di.BookService;

/**
 * @author weihu
 * @date 2018/8/8/008 23:48
 * @desc
 */
public class BookServiceImpl implements BookService {

    //方式1:之前,接口=实现类
    //    private BookDao bookDao = new BookDaoImpl();
    //方式二:接口+setter
    private BookDao bookDao;//has a 依赖关系

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void addBook() {
        //调用依赖对象中的save方法,会执行实现类的方法
        this.bookDao.save();

    }
}

 

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

    <!--
    模拟spring执行过程
        创建service实例:BookService bookService = new BookServiceImpl()    IoC  <bean>
        创建dao实例:BookDao bookDao = new BookDaoImpl()            IoC
        将dao设置给service:bookService.setBookDao(bookDao);        DI   <property>

        <property> 用于进行属性注入
            name: bean的属性名,通过setter方法获得
                setBookDao ##> BookDao  ##> bookDao
            ref :另一个bean的id值的引用

            BookServiceImpl依赖private BookDao bookDao;对象,ref引用bookDao的实现类bean
     -->
    <bean id="bookServiceId" class="com.jd.di.impl.BookServiceImpl">
        <property name="bookDao" ref="booDaoId"></property>
    </bean>

    <!--创建dao实例-->
    <bean id="booDaoId" class="com.jd.di.impl.BookDaoImpl"></bean>

</beans>

 

3.测试类

BookTest.java
package com.jd.test;

import com.jd.di.BookDao;
import com.jd.di.BookService;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author weihu
 * @date 2018/8/9/009 0:20
 * @desc
 */
public class BookTest {

    @Test
    public void BooTestDi(){
        String xmlPath="com/jd/di/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        BookService bookService = (BookService) applicationContext.getBean("bookServiceId");
        /**
         * @Override
         *     public void addBook() {
         *         //调用依赖对象中的save方法,会执行实现类的方法
         *         this.bookDao.save();
         *
         *     }
         */
        bookService.addBook();//最后执行的是save方法
    }
}