Introduction to IOC and Di of the spring framework

What is Spring Framework : Spring Framework is an open-source Java application framework designed to reduce the complexity of enterprise-level application development. It provides a comprehensive set of infrastructure support, including inversion of control (IoC), dependency injection (DI), AOP, data access, transaction management, Web application development and other functions. The design concept of the Spring framework is based on the principle of "simple, consistent, and efficient", and it is highly scalable and flexible, and can be seamlessly integrated with other open source technologies and tools. It has become one of the de facto standards for Java enterprise application development.

The concept of Ioc:

IOC (Inversion of Control) is the inversion of control, which is one of the core concepts of the Spring framework. In the traditional program development mode, programmers use the new keyword to create objects, call methods, etc. to control the life cycle and behavior of objects. In the IoC mode, the control is transferred from the programmer to the container, and the container is responsible for creating, managing and controlling the life cycle and behavior of the object.

The implementation of IoC is Dependency Injection (DI for short), which solves the coupling between classes by injecting object dependencies, making the program more flexible, scalable and easy to maintain. The use of IoC and DI can effectively reduce the coupling degree of the code, improve the reusability and testability of the code, thereby improving the development efficiency and quality.

The concept of Di:

DI (Dependency Injection), that is, dependency injection, is a way to realize IoC. It reduces the coupling between objects and improves the maintainability, reusability and testability of the code by handing over the dependencies between objects to external containers for management.

In DI, programmers no longer directly create objects through the new keyword, but through constructors, factory methods, or property injection, and the Spring container is responsible for creating corresponding objects and injecting them where they are needed. The advantage of this is that the dependencies between objects are clearly declared, and the dependencies can be easily replaced or modified without affecting other parts of the code.

The Spring framework provides a variety of injection methods, including constructor injection, setter method injection, field injection, etc. You can choose the appropriate injection method according to the actual situation.

case:

1. Import the jar package

 2. Create a hierarchical structure and write interfaces and implementation classes

dao layer 

interface:

package com.liu.dao;

public interface BookDao {
    void opp();
}

Implementation class:

package com.liu.dao;

public class BookDaoInpl implements BookDao{
    @Override
    public void opp() {
        System.out.println("opp-------");
    }
}

serivce layer

interface:

package com.liu.serivce;

public interface BookSreivce {
    void add();
}

impl layer (implementation class)

package com.liu.serivce.impl;

import com.liu.serivce.BookSreivce;

/**
 * 该类为实现了"BookSreivce"接口的服务实现类
 */
public class BookSerivceImpl implements BookSreivce {

    /**
     * 实现了"BookSreivce"接口中定义的"add()"方法
     */
    @Override
    public void add() {
        System.out.println("add----- impl"); 
    }
}
package com.liu.serivce.impl;

import com.liu.dao.BookDao;
import com.liu.serivce.BookSreivce;

/**
 * 该类实现了"BookSreivce"接口,用于对书籍进行添加等操作,
 * 它引入了"BookDao",在"add()"方法中使用"bookDao"。
 */
public class BookSerivceVip implements BookSreivce {

    /**
     * 返回一个"BookDao"对象
     * @return "BookDao"对象
     */
    public BookDao getBookDao() {
        return bookDao;
    }

    /**
     * 设置"BookDao"对象
     * @param bookDao 要设置的"BookDao"对象
     */
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    BookDao bookDao; // 声明一个"BookDao"类型的变量

    /**
     * 实现了"BookSreivce"接口中定义的"add()"方法,
     */
    @Override
    public void add() {
        System.out.println("Vip--------add");
        bookDao.opp();
    }
}

3. Configure the 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">
       
    <!-- 定义一个名为"bookDaoInpl"的bean -->
    <bean id="bookDaoInpl" class="com.liu.dao.BookDaoInpl"/>
    
    <!-- 定义一个名为"bookSerivce"的bean,并引用"bookDaoInpl" -->
    <bean id="bookSerivce" class="com.liu.serivce.impl.BookSerivceVip">
        <property name="bookDao" ref="bookDaoInpl"/> 
    </bean>
</beans>

4. servlet layer (test class)

package com.liu.servlet;

import com.liu.serivce.BookSreivce;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 该类用于测试"BookSreivce"
 */
public class Test01 {
    BookSreivce bookSreivce; // 声明一个"BookSreivce"类型的变量

    /**
     * 测试方法,用于检测"BookSreivce"
     */
    @Test
    public void Test01(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // 加载Spring配置文件
        bookSreivce = (BookSreivce) context.getBean("bookSerivce"); // 获取"bookSerivce" bean
        bookSreivce.add(); // 调用"add()"方法
    }
}

Test Results:

When the class of the core configuration file bookSerivce is specified as com.liu.serivce.impl.BookSerivceImpl

When the class of the core configuration file bookSerivce is specified as com.liu.serivce.impl.BookSerivceVip 

 

Guess you like

Origin blog.csdn.net/weixin_69420643/article/details/129756316