I: traditional SpringMVC engineering construction steps

Before getting familiar with the Spring Boot project, first build a Spring + SpringMVC project, which requires the following steps:

  • When introducing Spring dependencies, many Spring dependencies are introduced, such as spring-core, spring-Context, spring-context-support, spring-webMVC, etc
  • To configure web. XML, configure the contextConfigLocation parameter, configure the Spring listener, ContextLoaderListener, and configure the SpringMVC DispatcherServlet
  • Configure spring – servlet. XML, configuration for SpringMVC corresponding internal resources view recognizer InternalResourceViewResolver prefix, suffix, etc
  • Configure applicationContext. XML to configure the Spring core configuration file
  • Once configured, write Controller and start Tomcat

Two: specific steps

1. Introduce Spring dependencies

< the properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > < log4j. Version > 2.5 < / log4j version > < JSTL version > 1.2 < / JSTL. Version > < spring. The version > 4.2.3 RELEASE < / spring. Version > < / properties > < the dependency > <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency>Copy the code

2. Configuration web. XML

<! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc., / / DTD Web Application / 2.3 / EN "" http://java.sun.com/dtd/web-app_2_3.dtd" > < Web - app > < the display - the name > Archetype Created  Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring/applicationContext.xml</param-value> </context-param> <listener> <description>Spring listener </description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code

3. The configuration of the WEB – INF/spring – servlet. XML

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> < context: component - scan base-package="com.mengday.platform.web.controller" /> <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <! --> <property name="suffix" value=".jsp"></property> <! -- View file suffix --> <! What is the display of the view? Here is a JSP. Still can use, such as velocity -- > < property name = "viewClass" value = "org. Springframework. Web. Servlet. The JstlView" > < / property > < / bean >  </beans>Copy the code

4. applicatonContext.xml

<? The 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-4.2.xsd "> < / beans >Copy the code

5. HelloWorldController

@Controller public class HelloWorldController { @RequestMapping("/index") public String index(){ return "index"; }}Copy the code

6. Configure and start Tomcat in IntelliJ IDEA


Three:

  • There are a lot of dependencies to add in POM.xml (subtext: Can you add fewer dependencies?).

  • Configuration files such as web. XML, Spring-servlet. XML, and ApplicatonContext. XML need to be configured. You can see that these three files need to be configured just to integrate the functionality of an MVC. So many configuration files), and almost all these configuration in each project is the same configuration, only the individual value different (the implication: can not configuration, let the framework to configure, specific configuration values using gm’s common value, if the default value does not meet the conditions, to provide a configuration file to let the outside world to overwrite, adopt the way of contract is greater than the configuration)

  • Configure external Tomcat (translation: Also configure Tomcat, can you run the main method like a normal Java program?)

Disadvantages of Spring: Cumbersome configuration, various XML and Annotation configurations, and difficult to find out why things go wrong.


Create a Spring Boot project


Spring Boot projects are also based on Maven or Gradle. There are two ways to create a project: one is to create a project from an IDE (the IDE is also based on the website, but the IDE automatically downloads and opens it for you), and the other is to create a project from the website. Github.com/spring-proj…

# Method 1: Use Spring Initializer for IntelliJ IDEA

@RestController public class HelloWorldController { @RequestMapping("/index") public String index(){ return "suceess"; }}Copy the code

Go to http://localhost:8080/index

Method 2:start.spring.io

In your browser, type “start.spring. IO” in “Web” to add SpringMVC to your Dependencies list. Then generate the project (i.e. download the project) and open it using IDEA

In this way, you need to go to the official website, download the project, and then use idea Open project. If you use IDEA, you do not need these steps, and it will automatically download and open the project for you.

You can see the difference between the traditional SpringMVC integration approach and the build approach using Spring Boot. The biggest characteristic is that it’s fast, there’s not so much configuration, you can say zero configuration, unlike traditional SpringMVC projects where you have to configure a lot of stuff, even though it’s copied from somewhere else, It takes a few minutes to complete, it can go wrong, it’s all manual work, it takes a few seconds to create a Spring Boot project, it doesn’t go wrong, it doesn’t require any configuration.

The Spring Boot project is designed to solve the problem of cumbersome Spring configuration. The maximum implementation conventions are greater than the configuration (convention over Configuration), the project is built as fast as possible, and the XML configuration is replaced by annotation configuration.


The difference between the two methods:

Comparison of traditional Maven-archetype-WebApp and Spring Boot project structure

  • Different packing methods:
    • Traditional Web Maven projects use the Maven-archetype-WebApp skeleton and are packaged as war packages
    • Spring Boot is packaged using jar packages
  • The dependencies introduced in POM.xml are different
    • Traditional Web projects introduce multiple separate dependencies
    • Spring Boot is the introduction of spring-boot-starter. In Spring Boot, most dependencies do not need to specify version because the version number is already defined in spring-boot-starter
  • Different project directory structures:
    • Traditional Web projects don’t have classes under SRC /main/ Java, The Spring Boot Project has a startup class (Project name +Application) and a test class (Project name +ApplicationTest) in SRC /test/ Java.
    • Traditional Web projects have SRC /main/webapp/ web-INF /web.xml. There is no web.xml in Spring Boot
    • The traditional Web project Resources directory is empty. The Spring Boot project resources has the static and templates directories and a configuration file application.properties
  • Projects run differently
    • A traditional Web project is to start Tomcat
    • The Spring Boot project can run the main method directly or run jar directly (java-jar .jar).

Advantages of Spring Boot

  • The integration framework is very simple. For example, integrating SpringMVC requires only one dependency, spring-boot-starter-Web, and does not require any configuration, making the integration very quick and convenient. Spring Boot supports many common framework integration, such as log, Test, Mybatis, NOSQL, MQ, Template technology (Thymeleaf, freemark), JPA, AOP, actuator, etc. For details, see Starter POMs and Starter POMs

  • The number of imported dependencies is small. For example, to introduce test dependencies such as JUnit, Hamcrest, and Mockito, only spring-boot-starter-test is required

  • Automatic configuration, using the default configuration, no longer requires configuration files such as applicationContext.xml

  • Support for custom configurations, which can be configured in the application.yml or Config classes. If custom values are used, the default values are used

  • It’s easier to run, using the Java-jar command directly, or running the main method directly from the IDE

  • Built-in operation and maintenance of the work

Disadvantages of Spring Boot:

  • High encapsulation, problems are not easy to troubleshoot, suitable for the development of surprise siege lion, not suitable for beginners, beginners easy to use, once there is a problem is difficult to troubleshoot.
  • Converting existing or traditional Spring Framework projects into Spring Boot applications can be a difficult and time-consuming process. It only works with new Spring projects.
  • There’s less information. There’s less documentation
  • Spring Boot is evolving rapidly, and may be subject to major changes