Introduction and use of Spring basic framework

Modularity.

The evolution of spring

Single application architecture

​ When the website traffic is very small, only one application is needed and all functions are deployed together to reduce deployment nodes and costs. At this time, the data access framework (ORM) used to simplify the workload of adding, deleting, modifying and checking is the key.

​ Vertical application architecture

​ When the number of visits gradually increases, the acceleration caused by the addition of a single application to the machine becomes smaller and smaller. One of the ways to improve efficiency is to split the application into several unrelated applications to improve efficiency. At this time, the web framework (MVC) used to accelerate the development of front-end pages is the key.

​ Distributed service architecture

​ When there are more and more vertical applications, interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed, so that front-end applications can respond more quickly to changing market demands. At this time, the Distributed Service Framework (RPC) for improving business reuse and integration is the key.

​ Mobile computing architecture

​ When there are more and more services, the evaluation of capacity, the waste of small service resources and other problems gradually appear. At this time, a dispatch center needs to be added to manage the cluster capacity in real time based on access pressure to improve cluster utilization. At this time, the resource scheduling and governance center (SOA) used to improve machine utilization is the key.

 

The evolution of Java mainstream framework

​ 1、JSP+Servlet(server+applet)+JavaBean

  • jsp can be directly embedded in java code

​ 2, MVC three-tier architecture

  • DAO layers: entity, bean, po, vo, bo, pojo

​ 3. Use EJB for application development, but EJB is a heavyweight framework (when it is used, there are too many interfaces and dependencies, and it is very intrusive), which is more troublesome to use

​ 4、Struts1/Struts2+Hibernate+Spring

​ 5、SpringMVC+Mybatis+Spring

​ 6, SpringBoot development, the agreement is greater than the configuration

Core explanation

  • ​ Spring is an open source framework.
  • ​ Spring was born to simplify enterprise development, making development more elegant and concise.
  • ​ Spring is a container framework for IOC and AOP .
  • ​ IOC: Inversion of Control
  • ​ AOP: aspect-oriented programming
  • ​ Container: Contains and manages the life cycle of application objects, just like using a bucket of water, spring is a bucket, and the object is water

Advantages of using spring

  • ​ 1. Spring simplifies enterprise-level Java development through DI, AOP, and elimination of boilerplate code
  • ​ 2. In addition to the Spring framework, there is a huge ecosystem built on the core framework, which extends Spring to different fields, such as Web services, REST, mobile development, and NoSQL
  • ​ 3. Low intrusion design, code pollution is extremely low
  • ​ 4. Independent of various application servers, applications based on the Spring framework can truly realize the promise of Write Once, Run Anywhere
  • ​ 5. Spring's IoC container reduces the complexity of business object replacement and improves the decoupling between components
  • ​ 6. Spring's AOP support allows centralized processing of some common tasks such as security, transactions, logs, etc., thereby providing better reuse
  • ​ 7. Spring's ORM and DAO provide good integration with third-party persistence layer frameworks, and simplify the underlying database access
  • ​ 8. Spring's high degree of openness does not force applications to completely depend on Spring. Developers are free to choose part or all of the Spring framework.

 

3. IOC (Inversion of Control): Inversion of Control

Why introduce IOC

Create an ordinary java project to complete the following functions

UserDao.java

package com.mashibing.dao;

public interface UserDao {
    public void getUser();
}

UserDaoImpl.java

package com.mashibing.dao.impl;

import com.mashibing.dao.UserDao;

public class UserDaoImpl  implements UserDao {
    @Override
    public void getUser() {
        System.out.println("获取用户数据");
    }
}

UserService.java

package com.mashibing.service;

public interface UserService {
    public void getUser();
}

UserServiceImpl.java

package com.mashibing.service.impl;

import com.mashibing.dao.UserDao;
import com.mashibing.dao.impl.UserDaoImpl;
import com.mashibing.dao.impl.UserDaoMysqlImpl;
import com.mashibing.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public void getUser() {
        userDao.getUser();
    }
}

SpringDemoTest.java

package com.mashibing.test;

import com.mashibing.service.UserService;
import com.mashibing.service.impl.UserServiceImpl;

public class SpringDemoTest {
    public static void main(String[] args) {
       UserService service = new UserServiceImpl();
       service.getUser();
    }
}

In the previous code writing process, we all completed our functions in this way, but what if we add a UserDao implementation class?

UserDaoMysqlImpl.java

package com.mashibing.dao.impl;

import com.mashibing.dao.UserDao;

public class UserDaoMysqlImpl implements UserDao {
    @Override
    public void getUser() {
        System.out.println("mysql");
    }
}

If we want to use mysql, then we must modify the code of UserServiceImpl.java:

package com.mashibing.service.impl;

import com.mashibing.dao.UserDao;
import com.mashibing.dao.impl.UserDaoImpl;
import com.mashibing.dao.impl.UserDaoMysqlImpl;
import com.mashibing.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public void getUser() {
        userDao.getUser();
    }
}

But what if we add another oracle class?

UserDaoOracleImpl.java

package com.mashibing.dao.impl;

import com.mashibing.dao.UserDao;

public class UserDaoOracleImpl implements UserDao {
    @Override
    public void getUser() {
        System.out.println("oracle");
    }
}

At this time, UserService still has to continue to modify. Obviously, this method is no longer suitable for our needs, so how to solve it, you can use the following method

UserServiceImpl.java

package com.mashibing.service.impl;

import com.mashibing.dao.UserDao;
import com.mashibing.dao.impl.UserDaoImpl;
import com.mashibing.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
    @Override
    public void getUser() {
        userDao.getUser();
    }
}

Test class SpringDemoTest.java

package com.mashibing.test;

import com.mashibing.dao.impl.UserDaoMysqlImpl;
import com.mashibing.dao.impl.UserDaoOracleImpl;
import com.mashibing.service.UserService;
import com.mashibing.service.impl.UserServiceImpl;

public class SpringDemoTest {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        userService.setUserDao(new UserDaoMysqlImpl());
        userService.getUser();

        userService.setUserDao(new UserDaoOracleImpl());
        userService.getUser();
    }
}

In fact, from the code just now, everyone should be able to appreciate the importance of decoupling. Let's start to learn Spring's IOC.

IOC initial

Ioc is a kind of thought, DI is the way of implementation ( IOC and DI describe the same thing from different perspectives, IOC is described from the perspective of the container, and DI is described from the perspective of the application )

​ If you want to understand IOC, you need to clarify the following questions:

1. Who controls who
2. Controls what
3. What is reversal
4. Which aspects are reversed

If this process is more difficult to understand, then you can imagine the process of finding a girlfriend and a matchmaking company. If this process can be understood, then we will now answer the above questions:

1. Who controls who: In the previous coding process, all the objects were needed to create what objects themselves, and the programmers controlled the objects themselves. With the IOC container, it will become the IOC container to control the object.
2. What to control: the objects needed in the implementation process and the objects that need to be relied upon.
3. What is reversal: before there was no IOC container, we all actively created dependent objects in the object. This is a forward rotation, and there is After the IOC, the dependent objects are directly created by the IOC container and injected into the objects, from active creation to passive acceptance. This is the reversal
4. Which aspects are reversed: dependent objects

basic concept

    IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern.
    IOC is similar to the well-known dependency injection. This is a process of injecting objects through dependency. That is to say, the objects they use are through constructor parameters, factory method parameters or this is from the factory method constructor or The properties set by the object instance of the return value, and then the container injects these required dependencies when creating the bean. This process is reversed from the normal process of creating objects (hence the name IoC). The bean itself controls the instantiation or location of dependencies by directly constructing classes, or provides mechanisms such as the service locator pattern.

IN 与 IOC

Many people say that IOC and DI are the same thing. Generally speaking, there is no problem, but there are differences in essence. I hope everyone can be more rigorous. IOC and DI describe the same thing from different angles. IOC It is described from the perspective of the container, while DI is described from the perspective of the application. It can also be said that IOC is the design idea, and DI is the specific implementation

4. Summary

​ In the summary here, I hope you can understand two things:

1, decoupling

​ In an object-oriented design software system, the underlying implementation is composed of N objects, and all objects cooperate with each other to finally realize the business logic of the system.

​ It should be noted that in such a combination relationship, once a certain object has a problem, then other objects must be affected. This is because the coupling is too high, but the coupling relationship of the objects is unavoidable. necessary. As applications become larger and larger, the coupling of objects may become more and more complex, and multiple dependencies are often required. Therefore, both architects and programmers need to reduce the coupling of these objects when faced with such scenarios. Sex.

​ The coupling relationship is not only between the object and the object, but also between the various modules of the software system. It is a problem that we need to focus on to solve. In order to solve the problem of excessive coupling between objects, we can achieve decoupling between objects through IOC. The spring framework is the most widely used application of IOC theory.

​ As you can see from the above figure, when a third-party container is introduced, there is no coupling relationship between several objects. All objects are controlled by the container. This container is equivalent to the adhesive, which will Objects are glued together to play a role.

2, ecological

​ If any language or any framework wants to be invincible, the most important thing is its ecology.

 

Guess you like

Origin blog.csdn.net/zw764987243/article/details/111027648