[Turn] Spring, Spring MVC and Spring Boot difference

For a Java developers, Spring can be described as outsize, whether it is Spring Framework, Spring still lead the IOC, AOP style, have a profound influence on the subsequent development of Java generated at the same time, Spring always timely response to the needs of the community developers, the introduction of new features particular to adapt to the trend of development; but for most developers, usually the most contact should be the Spring MVC and Spring Boot, and this article will be on Spring, Spring MVC and Spring Boot to do a general overview and analysis described them each wants to solve the problem, so that beginners can better understand the concepts and Spring

本篇 structure 

This article deals broadly divided into four sections

  • What is Spring? It solves the problem?
  • What is SpringMVC? It solves the problem?
  • What is Spring Boot? It solves the problem?
  • Spring, Spring MVC, Spring Boot three comparison

What is Spring? It solves the problem?

We said Spring, generally refers to a Spring Framework, which is an open source application framework that provides a simple way of development, this development approach will avoid that may cause the code becomes complicated mess of a lot of business / tool objects, put it more plainly is a framework to help you manage these objects, including its creation, destruction, etc., such as Spring-based projects that often can see Bean, the object is governed by the Spring it represents.

In between managed objects and business logic, Spring by IOC (Inversion of Control) bridge set up for use, IOC can also be seen as the core Spring most important ideas, through IOC can bring what good is it? First look at a typical application scenarios of actual development, suppose we have a MVC-based application hierarchy, by the external controller provides the interface layer, and to provide an implementation through service layer, service layer in a WelcomeServiceservice interface, general the case is by WelcomeService service = new WelcomeServiceImpl();creating an instance of and call:

public class WelcomeController {
    private WelcomeService service = new WelcomeServiceImpl();
 
    @RequestMapping("/welcome")
    public String welcome() {
    return service.retrieveWelcomeMessage();
    }
}

After calling and found everything normal at this time, to submit function needs to be tested, but due to the application environment and test environment distinction needs to be replaced WelcomeServiceImplas a MockWelcomeServiceImpl, to facilitate the test, how do? There is no other choice but to change the code: 

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

OK then test the code will change back this way too cumbersome, and highly invasive to the code;
see below how to implement Spring's IOC, first WelcomeServicehanded over Spring Management:

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

Then the service code specific to get the object through the Spring IOC:

public class WelcomeController {
    @Autowired
    private WelcomeService service;
 
    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

When tested, only need to change the configuration file, the WelcomeServicecorresponding implementation changed MockWelcomeServiceImplto:

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

In this way there is no intrusion of any business code, it is effective to achieve loose coupling, we all know that the code is tightly coupled nightmare business development; the same time, Spring IOC offers far more than these, such as the reduction of useless objects created by Singleton, lazily initialization cost optimization

Of course, Spring core functionality far I do not know these, such as:

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

In fact, not by the Spring framework to achieve these functions can still be specific, but Spring provides a more elegant abstract interface to facilitate assembly of these features, while giving each embodied in a flexible configuration; in addition, based on Spring, you can easily integration with other frameworks, such as hibernate, ibatisetc., Spring official principle is never repeated-create the wheel, there is a good solution can only be integrated through Spring. Spring structure overview, you will find the Spring Framework itself does not provide too many specific functions, it is mainly focused on making your project code organization more elegant, it has excellent flexibility and scalability, while by Spring integration of the industry's best solutions, would like to understand the core Spring's implementation mechanism can refer tiny spring project

What is Spring MVC? It solves the problem?

Spring MVC is part of the Spring, and later Spring came out, we feel very good, so in accordance with this model designed an MVC framework (Spring with some of decoupling components), mainly for the development of WEB applications and network interfaces, it is Spring a module by Dispatcher Servlet, ModelAndView and View Resolver, make application development becomes very easy, a typical Spring MVC application development is divided into the following steps:
first through a configuration file statement Dispatcher Servlet:

<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>

 Statement by the configuration file servlet information, such as MVC resource, data source, bean, etc.

<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>

 To add other functions, such as Security, you need to add the corresponding configuration:

<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>

Increase service code, such as a controller, service, model, and finally generates war package, be activated by the container

What is Spring Boot? It solves the problem?

Early Spring in the form of code plus configuration provides good flexibility and scalability for the project, but with the ever-growing Spring, its profile has become increasingly complicated, too complex xml file has also been the Spring criticized places, especially in recent years other simple WEB program after another, such as Python-based or node.Js, a few lines of code to implement a WEB server, contrast, began to feel that a Spring too cumbersome, this when, Spring community launched a Spring Boot, its purpose is to achieve automatic configuration, reduce the complexity of building projects, such as the need to build an interface services through Spring Boot, a few lines of code can be realized, see the code example:

//引入spring-boot-starter-web依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
// declare Spring Boot application, business logic can be written directly 
@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); 
    } 
}

You do not even have additional WEB container, can be executed directly generate jar package, because spring-boot-starter-webthe module contains a built-in tomcat, you can directly use the container; based Spring Boot, the original configuration is not to say no, but a set of Spring Boot the default configuration, we can see it as a more general agreement, but also the Spring Boot followed convention over configuration principle, at the same time, if you need to use a variety of Spring in the past provided complex but powerful configuration capabilities, as Spring Boot stand by

In Spring Boot, you will find all your packages are introduced starter forms, such as:

  • spring-boot-starter-web-servicesFor SOAP Web Services
  • spring-boot-starter-webFor Web application and network interface
  • spring-boot-starter-jdbcFor JDBC
  • spring-boot-starter-data-jpa, Based on the persistence framework hibernate
  • spring-boot-starter-cacheFor cache support

and many more

Spring Boot interpretation 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

Italian translation of this sentence is:

Starters are dependent on a series of extremely convenient description, by including these starter in your project, you can get one-stop service you need, without having to copy as various examples of configurations and code, and then debug as in the past, really do with the box; Spring JPA example you want to use for data operations, only the introduction of spring-boot-starter-data-jpa to your project dependencies

Spring, Spring MVC, Spring Boot three comparison

In fact, I write to you, many readers should be clear, these three different areas of focus, problem-solving are not the same; in general, Spring is like a big family, there are a number of derivative products such as Boot, Security, JPA, etc. . But they are the basis of the IOC and Spring AOP, IOC provides dependency injection container, while the AOP addresses the aspect-oriented programming, and then on the basis of both, to achieve the advanced features of other derivative products; Spring MVC is based Servlet of a MVC framework, mainly to solve the problem of WEB development, because the Spring configuration is very complex, a variety of xml, properties process more cumbersome. So in order to simplify the use of the developer, Spring community creatively launched the Spring Boot, it follows the convention over configuration, which greatly reduces the threshold for using Spring, but without losing the original Spring flexible and powerful features, the following described by a picture three relationships:

The last sentence summary: Spring MVC and Spring Boot belong Spring, Spring MVC is based on a Spring MVC framework, Spring Boot is a fast and Spring-based integrated development package 

Original link: https://www.jianshu.com/p/42620a0a2c33

Guess you like

Origin www.cnblogs.com/zouwangblog/p/10985021.html