This article source: making here | | GitEE, click here

I. IOC inversion of control

1. IOC container idea

The object coupling relationship in Java system is very complex, and the dependence between each module of the system and the mutual call request between microservice modules are the same truth. Reducing the coupling degree between system modules, objects and microservices is one of the core problems in software engineering. Because the core idea of the Spring framework is IOC inversion of control, used to achieve decoupling between objects.

2. Inversion of control

  • The traditional way

Object A has active control over object B if it wants to use object B’s functional methods by creating instances of object B when needed and calling the required methods.

  • The IOC container

When the IOC container is used, the direct connection between object A and object B is lost. If object A wants to use the function method of object B, the IOC container will automatically create an instance of object B and inject it into the function module required by object A. In this way, object A loses active control, that is, inversion of control.

Dependency injection

IOC directly establishes relationships with objects, which is called DI Dependency Injection. Dependency: Object A depends on object B if object A needs to use object B’s functions. Injection: Instantiate object B in object A to use object B’s functionality. This action is called injection.

Ii. IOC container cases

1. Buying tickets for the bus

  • Simple ride
Public class ByBus {private BuyTicket BuyTicket = new BuyTicket (); private BuyTicket buyTicket ; public BuyTicketgetBuyTicket() {
        return buyTicket;
    }
    public void setBuyTicket(BuyTicket buyTicket) {
        this.buyTicket = buyTicket;
    }
    public void takeBus (){
        String myTicket = this.getBuyTicket().getTicket() ;
        if (myTicket.equals("ticket")){
            System.out.println("Ride"); }}}Copy the code
  • Simple ticket category
public class BuyTicket {
    public String getTicket() {return "ticket"; }}Copy the code

2. Spring configuration files

Here we use the Spring configuration file to inject the ticket class into the ride class to complete the action.

<bean id="byBus" class="com.spring.mvc.entity.ByBus">
    <property name="buyTicket" ref="buyTicket" />
</bean>
<bean id="buyTicket" class="com.spring.mvc.entity.BuyTicket"/>
Copy the code

Test your code

public class Test1 {
    @Test
    public void test01 (){
        ApplicationContext context = 
        new ClassPathXmlApplicationContext("/ioc-contain-01.xml");
        ByBus byBus = (ByBus) context.getBean("byBus"); byBus.takeBus(); }}Copy the code

Iii. Summary of core API

The core apis used above will be explained and summarized.

1, the BeanFactory

This is a factory for generating arbitrary beans. With lazy loading, the Bean is initialized the first time it is getBean.

2, ApplicationContext

BeanFactory is a more powerful subinterface to BeanFactory. (Internationalization handling, event passing, Bean autowiring, Context implementation of various application layers). When the configuration file is loaded, the object is instantiated.

3, ClassPathXmlApplicationContext

Load XML runtime location: /WEB-INF/classes/… xml

4, FileSystemXmlApplicationContext

Load XML runtime location: / web-INF /… XML, through the ServletContext. GetRealPath () to obtain the specific drive configuration.

Source code address

Making address GitEE, https://github.com/cicadasmile/spring-mvc-parent, https://gitee.com/cicadasmile/spring-mvc-parentCopy the code