springboot: Before entering makers need to realize that the book, you know?

In the previous article had Damon said SSM is the more popular development template, but in fact in many large companies in the development, mainstream Springboot is the mainstream. These companies chose to use springboot of reasons, not just because the advantage is without any threshold for the novice, as long as know Maven will be able to see the document every step of starting a new project.
And for some experts, the configuration is changed every minute thing. Although Spring Boot the auto configuration loading really around, but people familiar with spring still pretty easy to find where the problem lies. In addition fat jar packaging way for the deployment really become very beautiful. devtools is so notorious change the code to be restarted half history. So today we talk about Damon just springboot
First, we know that Boot is meant to start in the computer, Spring Boot icon in the spring is just a start switch, meaning that it provides a one-stop spring is quick to use solutions.
The Spring Boot Spring community is a relatively new project. The aim of the project is to help developers more easily create Spring-based applications and services, so that more people can be faster entry of Spring experience, so Java developers can achieve production efficiency Ruby Rails as on. Providing a fixed, convention over configuration style frame for the Spring ecosystem.

Spring Boot has the following characteristics:

  • For the development of Spring-based experience to provide faster entry
  • Out of the box, there is no code generation, need not XML configuration. But you can also modify the default values ​​to meet specific needs.
  • It provides a number of large projects in common non-functional properties, such as embedded servers, security, index, health monitoring, external configuration.
  • Spring Boot Spring is not to enhance the functionality, but provides a way to quickly use the Spring.

A simple example

  • First, create a Maven project in general, and have a basic pom.xml src / main / java structure.

pom.xml file

  • Note: An error occurred pom.xml after the download is complete. Here are two cases
  • 1, if it is not downloaded some packages, you can specify to delete the local maven repository delete the specified folder, and then rebuild the project, let maven re-download package.

  • 2, if the following error occurs, the right project, [Maven] [Update Project Configuration ...] can be.

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
      
      	<groupId>com.etc</groupId>
      	<artifactId>SpringBoot</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>
      
      	<name>SpringBoot</name>
      	<description></description>
      
      	<parent>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-parent</artifactId>
      		<version>1.5.8.RELEASE</version>
      		<relativePath/> <!-- lookup parent from repository -->
      	</parent>
      
      	<properties>
      		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      		<java.version>1.8</java.version>
      	</properties>
      
      	<dependencies>
      		<!-- spring-boot-starter-*这里的*代表spring里头的某个功能 -->
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-web</artifactId>
      		</dependency>
      		<!-- 单元测试 -->
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-test</artifactId>
      			<scope>test</scope>
      		</dependency>
      		<!-- 启动热部署 -->
      		<dependency>
      	        <groupId>org.springframework.boot</groupId>
      	        <artifactId>spring-boot-devtools</artifactId>
      	        <scope>provided</scope>
      	        <optional>true</optional>
      	    </dependency>
      	</dependencies>
      
      	<build>
      		<plugins>
      			<plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      				<dependencies>
                          <dependency>
                              <groupId>org.springframework</groupId>
                              <artifactId>springloaded</artifactId>
                              <version>1.2.6.RELEASE</version>
                          </dependency>
                      </dependencies>
      			</plugin>
      		</plugins>
      	</build>
      </project>
    复制代码

Description of pom

  • The first is increased, increasing the parent pom relatively simple, but spring-boot-starter-parent contains a large number configured dependency management, add these dependencies in the project when the need to write their own version.

  • Use parent pom simple, but in some cases we have a parent pom, while not directly increasing by as follows:

      	<dependencyManagement>
      		     <dependencies>
      		        <dependency>
      		            <!-- Import dependency management from Spring Boot -->
      		            <groupId>org.springframework.boot</groupId>
      		            <artifactId>spring-boot-dependencies</artifactId>
      		            <version>1.2.3.RELEASE</version>
      		            <type>pom</type>
      		            <scope>import</scope><!—这个地方-->
      		        </dependency>
      		    </dependencies>
      		</dependencyManagement>
    复制代码

About java.version property

  • Although the above pom.xml this property does not appear, here to remind.

  • Spring default jdk1.6, if you want to use jdk1.8, you need to add java.version in pom.xml inside the property, as follows:

      <properties>
          <java.version>1.8</java.version>
      </properties>
    复制代码

Add spring-boot-starter-web-dependent

  • Spring by adding a spring-boot-starter- * This dependence can support a specific function.
  • Our ultimate goal is to achieve this example web features, so add that this dependence.
  • A more complete list of features can be viewed: Using-boot-starter-poms

Add spring-boot-maven-plugin plugin

  • The plug-in supports a variety of functions, commonly used in two, the first is to package the project executable jar package.
  • The implementation of the project root directory mvn package will generate an executable jar package jar package contains all the dependent jar package, which requires only a jar package can run the program easy to use. The command will be executed after a retention jar XXX.jar.original package comprising separate parts of the project.
  • After generating the executable jar package, the command line java -jar xxxx.jar to start the project.
  • Another command is mvn spring-boot: run, can be used directly tomcat (default) to start the project.
  • The second function is to heat deployed in our development process, we often need to modify, in order to avoid duplication start the project, we can enable hot deployment.

spring-loaded hot deployment

  • Spring-Loaded project provides a powerful hot deployment features, add / delete / modify the method / field / interface / enumeration code and other hot deployment time can be, very fast, very convenient.

  • Want to use this feature in the Spring Boot is very simple, it is to add rely on spring-boot-maven-plugin plugin below:

  •   		<dependency>
      		    <groupId>org.springframework</groupId>
      		<artifactId>springloaded</artifactId>
      		    <version>1.2.6.RELEASE</version>
      		</dependency>
      		<!--  支持热部署 eclipse推荐使用,注:该功能在dependencies中添加 -->
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-devtools</artifactId>
      			<scope>provided</scope>
      			<optional>true</optional>
      		</dependency>
    复制代码
  • After the addition, by mvn spring-boot: run start on the hot-deployed.

  • Note: When using hot deployment, the need to compile IDE class to take effect, you can turn on the automatic compilation, so you save the changes when the class is automatically reloaded.

to sum up:

As a new program ape, Damon hopes to progress together with you. Article or inadequacy described elsewhere, we hope to raise a lot of progress together.

Damon will continue to explore some useful advice, knowledge and new tools to share with you, thank you!

Past articles are uploaded to github, there is little interest in Star partner can under: github.com/xxxyyh/Fron...

Guess you like

Origin juejin.im/post/5d63f234f265da03d1555f70