Spring (Introduction to IOC/DI)

I. Overview

IOC: Inversion of control. In the past, objects needed to be created by themselves. Now the object creation operation is handed over to the spring framework. When using it, just ask the framework directly; spring is responsible for all objects and management, which is called IOC container.

DI: Dependency injection. A class A needs to execute the method in class B, that is, class A depends on class B. If class A wants to run normally, it needs to inject objects of class B; this is dependency injection; the spring framework automatically injects objects. The setter method is used by default.

2. Introduction to IOC

1. Create a project and import the jar packages required by spring

 As shown below:

2. Create interface class

2.1, dao layer

 The BookDao interface code is as follows:

public interface BookDao {
    public void save();
}

The BookDaoIMpl class code is as follows:

public class BookDaoImpl implements BookDao{

    @Override
    public void save() {
        System.out.println("BookDaoImpl...save");
    }
}

2.2. Service layer 

The BookService interface code is as follows:

public class BookDaoImpl implements BookDao{
    @Override
    public void save() {
        System.out.println("BookDaoImpl...save");
    }
}
The BookServiceImpl class code is as follows:
public class BookServiceImpl implements BookService {
    //BookServiceImpl依赖BookDao的对象
    BookDao bookDao;
    //setBookDao如果没有这个方法,框架就不能自动注入
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void save() {
        System.out.println("BookServiceImpl...save");
        bookDao.save();
    }
}
The BookServiceVip class code is as follows:
public class BookServiceVip implements BookService {
    //BookServiceVip依赖BookDao的对象
    BookDao bookDao;
    //setBookDao如果没有这个方法,框架就不能自动注入
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void save() {
        System.out.println("BookServiceVip...save");
        bookDao.save();
    }
}

3. Use spring to complete IOC configuration and DI dependency injection

3.1. Create spring’s core configuration file

<?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框架,创建管理哪些对象
          id: 给创建出来的对象起个名字
          class: 要创建对象对应类的全类名
    -->
    <!-- 实现BookServiceImpl依赖注入bookDao -->
    <bean id="bookDao" class="com.dou.dao.impl.BookDaoImpl"/>
    <bean id="bookService" class="com.dou.service.impl.BookServiceImpl">
        <!-- 通过property给类的属性进行注入数据
              常规类型使用value注入;
              对象使用tef引用上面定义的;-->
        <property name="bookDao" ref="bookDao"/>
    </bean>
</beans>

3.2. Dependency injection

Dependency injection has been annotated in BookServiceImpl and BookServiceVip, and they can be automatically injected through the setting method.

As shown below:

 

 4. Test

The test is divided into three steps:

1. Get the IOC container

2. Get the BookService object from the container

3. Execution method

The sample code is as follows:

public class Test01 {
    BookService bookService;
    @Test
    public void test01(){
        //对象创建由spring框架创建,此处只需要问IOC容器要
        //1、通过 ClassPathXmlApplicationContext加载spring.xml获得IOC容器
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2、从IOC容器中根据名字获取对象
        bookService = (BookService) classPathXmlApplicationContext.getBean("bookService");
        //3.执行方法
        bookService.save();
    }
}

Guess you like

Origin blog.csdn.net/m0_71385552/article/details/129756580
Recommended