general

The Spring framework is like a family, with many derivatives such as Boot, Security, JPA, and so on. But both are based on Spring’s IOC and AOP. Ioc provides a container for dependency injection, and AOP solves the problem of cross-cutting programming. And then on the basis of the two to achieve the advanced functions of other extension products.

Spring MVC is a Servlet-based MVC framework that addresses WEB development issues.

Later, due to the complexity of Spring configuration, various XML and JavaConfig processing is cumbersome. Therefore, in order to facilitate the use of developers, Spring BOOT was introduced to simplify the configuration process of Spring.

To put it more simply: Spring initially decouples application components using “factory pattern” (DI) and “proxy pattern” (AOP). People thought it worked so well that they built an MVC framework (some components decoupled by Spring) to develop Web applications, resulting in SpringMVC. Then, I found that I wrote a lot of boilerplate code for each development. In order to simplify the work process, I developed some “starter packages”, which is Spring BOOT.

Spring MVC functionality

Spring MVC provides a lightly coupled way to develop Web applications.

Spring MVC is a module of Spring, a Web framework. Developing Web applications is easy with Dispatcher servlets, ModelandViews, and View Resolver. The problem areas addressed are Web application or service development – URL routing, sessions, template engines, static Web resources, and so on.

Spring BOOT features

Spring Boot implements automatic configuration and reduces the complexity of project construction.

It is well known that the Spring framework requires a lot of configuration, and Spring Boot introduces the concept of automatic configuration to make project setup easy. Spring Boot itself does not provide the core features and extension functions of the Spring framework, but is used to develop a new generation of applications based on the Spring framework quickly and flexibly. That is, it is not intended as a solution to replace Spring, but rather as a tool that works closely with the Spring framework to enhance the Spring developer experience. It also integrates a large number of common third-party library configurations (such as Jackson, JDBC, Mongo, Redis, Mail, etc.), which can be configured almost out-of-the-box for Spring Boot applications. Most Spring Boot applications require very little configuration code, allowing developers to focus more on business logic.

Spring Boot is just a host to help you simplify the project setup process. If you host a WEB project and use Spring MVC as the MVC framework, the workflow is exactly the same as what you described above, because Spring MVC does this part of the work instead of Spring Boot.

When you switch to Spring Boot, the project initialization method changes, the configuration file changes, and there is no need to install a container server such as Tomcat. Maven simply runs as a website by printing jar packages, but your core business logic implementation and business process implementation remain the same.

So, the simplest way to summarize this is:

Spring is an “engine”;

Spring MVC is an MVC framework based on Spring;

Spring Boot is a rapid development integration package based on Spring4 conditional registration.

What is Spring? What problem does it solve?

When we say Spring, we generally refer to the Spring Framework, which is an open source application Framework that provides an easy way to develop without the large number of business/tool objects that can clutter your code. In more general terms, the framework manages these objects for you, including their creation, destruction, etc. For example, beans are commonly seen in Spring-based projects, which represent objects governed by Spring.

Spring Bridges the use of managed objects and business logic through IOC (Inversion of Control). IOC can also be regarded as the core and most important idea of Spring. What benefits can be gained from IOC? Let’s first look at a typical application scenario in actual development. Suppose we have an application based on MVC hierarchical structure, which provides interfaces through the Controller layer and implementation through the Service layer. In the Service layer, there is a WelcomeService service interface. WelcomeService service = new WelcomeServiceImpl() Create an instance and call:

public class WelcomeController {
    private WelcomeService service = new WelcomeServiceImpl();

    @RequestMapping("/welcome")
    public String welcome() {
    returnservice.retrieveWelcomeMessage(); }}Copy the code

The WelcomeServiceImpl is a MockWelcomeServiceImpl for testing purposes because the actual application environment is different from the test environment. There is no other option but to change the code:

public class WelcomeController { private WelcomeService service = new MockWelcomeServiceImpl(); . }Copy the code

Testing OK and then changing the code back is too cumbersome and intrusive; Here’s how to implement WelcomeService via Spring’s IOC, starting with Spring management:

<bean name="WelcomeService" class="XXX.XXX.XXX.service.impl.WelcomeServiceImpl"/>Copy the code

Then get the object in the business code via Spring IOC:

public class WelcomeController {
    @Autowired
    private WelcomeService service;

    @RequestMapping("/welcome")
    public String welcome() {
        returnservice.retrieveWelcomeMessage(); }}Copy the code

To test, simply change the configuration file and change the WelcomeService implementation to MockWelcomeServiceImpl:

<bean name="WelcomeService" class="XXX.XXX.XXX.service.impl.MockWelcomeServiceImpl"/>Copy the code

This approach does not intrude on business code, it effectively implements loose coupling, which everyone knows is a nightmare for business development; At the same time, Spring IOC offers much more, such as reducing the creation of useless objects through singletons, optimizing initialization costs through lazy loading, and so on

Of course, Spring’s core features are far from obvious, such as:

  • Spring AOP
  • Spring JDBC
  • Spring MVC
  • Spring ORM
  • Spring JMS
  • Spring Test

It is still possible to implement these features without the Spring framework, but Spring provides a more elegant abstract interface to facilitate the assembly of these features, while giving each implementation flexibility in configuration; In addition, based on Spring, you can easily integrate with other frameworks, such as Hibernate, IBatis, etc. The official principle of Spring is never to duplicate the wheel, a good solution only needs to integrate through Spring. If you look at the Spring structure, you’ll find that the Spring Framework itself doesn’t offer much in the way of specific functionality. It focuses on making your project code more elegant, making it extremely flexible and extensible, while integrating industry-leading solutions through Spring. See the Tiny Spring project for the core implementation mechanism of Spring

What is Spring MVC? What problem does it solve?

Spring MVC is a part of Spring. After Spring came out, we thought it was easy to use, so we designed an MVC framework (some components decoupled by Spring) according to this pattern, which is mainly used for developing WEB applications and network interfaces. It is a module of Spring. With Dispatcher Servlet, ModelAndView and View Resolver, application development is easy. A typical Spring MVC application development is divided into the following steps:

 <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>com.qgd.oms.web.common.mvc.OmsDispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>Copy the code

Declare servlet details, such as MVC Resource, data source, bean, etc., through configuration files

 <mvc:resources mapping="/css/**/*" location="/static/css/" cache-period="21600"/>
    <mvc:resources mapping="/js/**/*" location="/static/js/" cache-period="21600"/>
    <mvc:resources mapping="/views/**/*.html" location="/static/views/" cache-period="21600"/>
    <mvc:resources mapping="/fonts/**/*" location="/static/fonts/"/>
    <mvc:resources mapping="/ueditor/**/*" location="/static/js/lib/ueditor/"/>
    <mvc:resources mapping="/img/**/*" location="/static/img/"/>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="validationQuery" value="${jdbc.validationQuery}"/>
        <property name="maxTotal" value="10"/>
        <property name="minIdle" value="5"/>
        <property name="maxIdle" value="10"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="maxOpenPreparedStatements" value="50"/>
    </bean>

    <bean id="configService" class="com.qgd.oms.web.common.service.ConfigService">
        <property name="configStore">
            <bean class="com.qgd.oms.web.common.service.impl.DbConfigStore">
                <property name="dataSource" ref="dataSource"/>
                    <property name="taskScheduler" ref="defaultScheduler"/>
                <property name="refreshInterval" value="30000"/>
            </bean>
        </property>
    </bean>Copy the code

If you need to add other functions, such as security, you need to add corresponding configurations:

<http pattern="/css/**/*" security="none"/>
    <http pattern="/js/**/*" security="none"/>
    <http pattern="/views/**/*.html" security="none"/>
    <http pattern="/fonts/**/*" security="none"/>
    <http pattern="/ueditor/**/*" security="none"/>
    <http pattern="/img/**/*" security="none"/>

    <http use-expressions="true" entry-point-ref="omsAuthenticationEntryPoint">
        <logout logout-url="/omsmc/authentication/logout/*" success-handler-ref="omsLogoutSuccessHandler"></logout>
        <intercept-url pattern='/omsmc/authentication/login*' access="permitAll" />
        <intercept-url pattern='/ms/**/*' access="permitAll" />
        <intercept-url pattern='/ * *' access="authenticated"/ > <! --<security:form-login />--> <custom-filter ref="omsUsernamePasswordAuthenticationFilter" position="FORM_LOGIN_FILTER" />
        <remember-me services-ref="omsRememberMeServices" key="yfboms"/>
        <csrf disabled="true"/>
    </http>Copy the code

Add business code, such as Controller, service, model, etc., and finally generate a WAR package and start it through the container

What is Spring Boot? What problem does it solve?

In the early stage, Spring provided good flexibility and expansibility for the project in the form of code and configuration. However, as Spring became larger and larger, its configuration files became more and more complicated, and too many complex XML files have always been criticized by Spring, especially in recent years when other simple WEB solutions emerged one after another. For example, based on Python or Node.js, a few lines of code can implement a WEB server, by contrast, people gradually feel that the Spring set is too cumbersome, at this time, the Spring community launched Spring Boot, which aims to achieve automatic configuration, reduce the complexity of project construction. If you need to set up an interface service, a few lines of code can be implemented through Spring Boot. Please see the code example:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>Copy the code

@controller @springBootApplication public class MockServerApplication {@requestMapping ("/hi")
    @ResponseBody
    String home() {
        return "how are you!"; } public static void main(String[] args) { SpringApplication.run(MockServerApplication.class, args); }}Copy the code

You don’t even need an extra WEB container, just generate jar packages to execute, because the Spring-boot-starter-Web module includes a built-in Tomcat that can be used directly by the container; Based on Spring Boot, it does not mean that the original configuration is gone, but Spring Boot has a default configuration. We can regard it as a relatively general convention, and Spring Boot follows the principle of convention over configuration. At the same time, If you need to use a variety of complex but powerful configuration features Spring has traditionally provided, Spring Boot also supports them

In Spring Boot, you’ll find that all the packages you import are

starter

Form, such as:

  • spring-boot-starter-web-services, for SOAP Web Services
  • spring-boot-starter-web, for Web applications and network interfaces
  • spring-boot-starter-jdbcFor the JDBC
  • spring-boot-starter-data-jpa, based on Hibernate persistence layer framework
  • spring-boot-starter-cacheFor cache support
  • , etc.

Spring Boot explains starter as follows:

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go


The translation of this sentence is:

By including Starters in your project, you can go one stop and get exactly what you need, instead of copying sample configurations and code and debugging it out of the box. For example, if you want to use Spring JPA for data manipulation, just introduce spring-boot-starter-data-jPA into your project dependencies


Spring, Spring MVC, and Spring Boot

As many readers know by now, the three focus on different areas and solve different problems; In general, Spring is like a big family, with many derivative products such as Boot, Security, JPA and so on. But their foundation is Spring’s IOC, which provides a dependency injection container, and AOP, which addresses section-oriented programming and then builds on that foundation to implement the advanced functions of other derivatives; Spring MVC is an MVC framework based on servlets, mainly to solve the problems of WEB development, because the configuration of Spring is very complex, various XML and properties processing is cumbersome. Therefore, in order to simplify the use of developers, the Spring community creatively launched the Spring Boot, which follows convention over configuration, greatly reducing the barrier to use Spring, but without sacrificing the original flexible and powerful functions of Spring. Here is a diagram to illustrate the relationship between the three:



Spring MVC and Spring Boot both belong to Spring. Spring MVC is an MVC framework based on Spring, while Spring Boot is a set of rapid development integration packages based on Spring