Official document:http://projects.spring.io/spring-boot/
Project code: Https://github.com/chenxing12/springboot-test
First, Introduction
Springmvc is a very great framework for open source and rapid development. Good design is bound to divide and decouple. Therefore, Spring has many sub-projects, such as core, context, Bean, MVC, and so on. This is simple and clear to the people who know the foundation, but SPRINGMVC is invented for the operation of Fools. For beginners Springmvc, want to start on the development need to copy a series of dependency and do not know this is why, do not know is not less dependent. Like I just touch springmvc time everywhere Baidu tutorial and found each have different, so copied one after another code but can not set their own, the root cause is not aware of each dependent package.
Spring-boot is created to solve complex code configurations. Spring-boot is also based on Java-base development code, and without XML file configuration, all the code is done by Java. You can also join groovy's dynamic language execution.
Second, to build a basic WEB-MVC Project 2.1 Configure Environment
- Java 1.8+
- Maven 3.3+
- Spring-boot 1.3.5
- Idea 15
- Thymeleaf 3
2.2 Start
In idea, choose new-maven to create an empty MAVEN project, such as name Springboot-test.
2.2.1pom.xml
To set the Java version:
<properties> <java.version>1.8</java.version></properties>
Add dependent version Management Dependencymanagement
<dependencyManagement> <dependencies> <dependency> <!--Import dependency Management from Spring Boot-- <groupId>org.springframework.boot</groupId> <artifactid >spring-boot-dependencies</artifactId> <version>1.3.5.RELEASE</version> <type >pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Add Spring-web Project Dependencies
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools</ artifactid> <optional>true</optional> </dependency></dependencies>
Add Build-plugin
<build> <plugins> <plugin> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-maven-plugin</artifactId> <configuration> < fork>true</fork> </configuration> </plugin> </plugins></build>
In this way, a simple restful webservice project is built. If you want to support view rendering, that is, JSP, Freemark, Velocity, and so on, add the corresponding dependency. For example, I use the thymeleaf template:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-thymeleaf</artifactid></dependency>
2.2.2 Creating Java Code
If the name of the new project is: Springboot-test. The package springboot-test/src/main/java/com/test is created.
COM +-Example + myproject +-Application.java | +-Domain | +-Customer.java | +-Customerrepository.java | +-Service | +-Customerservice.java | +-Web +-Customercontroller.java
Com.test is our basic package name. The configuration class Com.test.AppConfig is created below.
Package Com.test;import Org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;/** * Created by Miaorf on 2016/6/19. */@SpringBootApplicationpublic class AppConfig {public static void Main (string[] args) { Springapplication.run (Appconfig.class); }}
@SpringBootApplication Callout Launch Configuration portal, you can find it started by a main method. The class that uses this annotation must be placed in the outermost package because it is scanned for packages below this class by default. Otherwise you need to configure @componentscan yourself.
In this way, the configuration is basically complete. The following development control layer controller:
Create a Com.test.controller.HelloController.
Package Com.test.controller;import Org.springframework.stereotype.controller;import Org.springframework.ui.Model; Import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import java.util.hashmap;import java.util.Map;/** * Created by Miaorf on 2016/6/19. */@Controllerpublic class Hellocontroller { @RequestMapping ("/index") public String index (model model) { Model.addattribute ("name", "Ryan"); Return "index"; } @RequestMapping ("/json") @ResponseBody public map<string,object> json () { map<string,object > map = new hashmap<string,object> (); Map.put ("name", "Ryan"); Map.put ("Age", "the"); Map.put ("Sex", "man"); return map; }}
To create a view code:
The view is placed by default in springboot-test\src\main\resources\templates**.
So create springboot-test\src\main\resources\templates\index.html
<! DOCTYPE html>
D:\workspace\springboot\springboot-test\src\main\webapp\hello.html
<! DOCTYPE html>
2.2.3 RunThere are many ways to start the main method, or you can start it from the command line:
D:\TEMP\SPRINGBOOT-TEST>MVN spring-Boot:run[info] Scanning forprojects ... [WARNING] [WARNING] Some problems were encountered whileBuilding the effective model forcom.test:springboot-test:jar:1.0-Snapshot[warning]' Build.plugins.plugin.version ' forOrg.springframework.boot:spring-boot-maven-plugin is missing. @ Line, column 21[warning][warning] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] for Thisreason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO]------------------------------------------------------------------------[INFO] Building springboot-test 1.0-Snapshot[info]------------------------------------------------------------------------[Info][info]>>> Spring-boot-maven-plugin:1.3.5.release:run (default-CLI) > Test-compile @ springboot-test >>>[Info][info]---maven-resources-plugin:2.6:resources (default-resources) @ springboot-test---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying2Resources[info][info]---maven-compiler-plugin:3.1:compile (default-compile) @ springboot-test---[INFO] nothing to compile-All classes is up to Date[info][info]---maven-resources-plugin:2.6:testresources (default-testresources) @ springboot-test---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Skip non existing resourcedirectory D:\temp\springboot-Test\src\test\resources[info][info]---maven-compiler-plugin:3.1:testcompile (default-testcompile) @ springboot-test---[INFO] No sources to Compile[info][info]<<< Spring-boot-maven-plugin:1.3.5.release:run (default-CLI) < Test-compile @ springboot-test <<<[Info][info]---spring-boot-maven-plugin:1.3.5.release:run (default-CLI) @ springboot-test---[INFO] attaching agents: []. ____ _ __ _ _ /\\/___ ' _ __ _ _ (_) _ __ _ \ \ \ (() \___ | ' _ | ' _| | ' _ \ _ ' | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____|. __|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/: : Spring Boot:: (v1.3.5.RELEASE)
View CodeBrowser access: Localhost:8080/index
Spring-boot-Preliminary construction