- Learn by using the spring Boot "1.5.11.RELEASE" Official document and record a bit of growth.
- About Spring Boot
For spring, you can easily create stand-alone, production-level Spring applications, run directly with Java-jar, and provide a command-line tool for the spring Boot CLI to perform "Spring Scripts".
Spring Boot Environment Dependent: Java 7 above, spring Framework 4.3.15.RELEASE above, Maven (3.2+), Gradle 2 (2.9 or later) and 3.
- Installing Maven | Gradle, and configure the Spring boot Hello World project
Spring boot provides a sample Pom.xml | Build.gradle, generally the spring boot project needs to inherit Groupid:org.springframework.boot.
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3 xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4 <modelversion>4.0.0</modelversion>5 6 <groupId>Com.example</groupId>7 <Artifactid>MyProject</Artifactid>8 <version>0.0.1-snapshot</version>9 Ten <!--Inherit defaults from Spring Boot - One <Parent> A <groupId>Org.springframework.boot</groupId> - <Artifactid>Spring-boot-starter-parent</Artifactid> - <version>1.5.11.RELEASE</version> the </Parent> - - <!--Add Typical dependencies for a Web application - - <Dependencies> + <Dependency> - <groupId>Org.springframework.boot</groupId> + <Artifactid>Spring-boot-starter-web</Artifactid> A </Dependency> at </Dependencies> - - <!--Package as an executable jar - - <Build> - <Plugins> - <plugin> in <groupId>Org.springframework.boot</groupId> - <Artifactid>Spring-boot-maven-plugin</Artifactid> to </plugin> + </Plugins> - </Build> the * </Project>
1 Plugins {2 ID ' org.springframework.boot ' version ' 1.5.11.RELEASE '3 id ' java '4 }5 6 7 Jar {8 baseName = ' MyProject '9 Version = ' 0.0.1-snapshot 'Ten } One A Repositories { - Jcenter () - } the - Dependencies { - compile ("Org.springframework.boot:spring-boot-starter-web") - testcompile ("Org.springframework.boot:spring-boot-starter-test") +}
- Spring Boot CLI command line [HTTPS docs.spring.io/spring-boot/docs/1.5.11.release/reference/htmlsingle/# GETTING-STARTED-INSTALLING-THE-CLI]
Spring Boot CLI supports execution of spring Script (. Groovy)
@RestControllerclass Thiswillactuallyrun { @RequestMapping ("/") String Home () { " Hello world! " }}
App.groovyExecute spring Run App.groovy to run App.groovy
- Build the first spring Boot
The first step: Create Pom.xml, can be configured according to the sample Pom.xml above, and then execute: MVN package to build the project environment, you can also import into the IDE to build the project environment;
The second step: execution: mvn dependency:tree update dependency, you can also use the IDE to update dependent operations;
Step three: Write the main class, Src/main/java/example.java
1 Importorg.springframework.boot.*;2 ImportOrg.springframework.boot.autoconfigure.*;3 ImportOrg.springframework.stereotype.*;4 Importorg.springframework.web.bind.annotation.*;5 6 @RestController7 @EnableAutoConfiguration8 Public classExample {9 Ten@RequestMapping ("/") One String Home () { A return"Hello world!"; - } - the Public Static voidMain (string[] args)throwsException { -Springapplication.run (Example.class, args); - } - +}
Fourth step: Debug Execution: MVN spring-boot:run run demo, open localhost:8080 can be accessed.
Fifth step: Build the jar to configure build, then execute: MVN package, then execute: Java-jar Target/myproject-0.0.1-snapshot.jar can also be run.
1 <Build>2 <Plugins>3 <plugin>4 <groupId>Org.springframework.boot</groupId>5 <Artifactid>Spring-boot-maven-plugin</Artifactid>6 </plugin>7 </Plugins>8 </Build>
Spring Boot's Hello World