20 minutes Quick Start Spring Boot Application
Spring the Boot (referred to as SB) for simplifying the configuration process Spring application.
A "habit over configuration" approach to develop
learning SPB is actually grasp its various constraints and requirements.
Learning Video address: http://www.itlaoqi.com/chapter/1647.html
Pre-preparation
- JDK 8 or later
- We recommended to install Intellj Idea Ultimate (Ultimate Edition)
We will use Maven and Spring MVC
Spring Boot directory structure
- / Java Java source code directory
- / Resources Resource Directory
- / Resources / static static resource directory
- / Resources / templates directory page presentation layer
- /resources/application.properties Spring Boot Profiles
/ Test test file directory
Spring Boot development summary
- Creating the Maven project, build the project structure
- Configuration pom.xml, references to simplify the configuration of various starter starter
- Configure operating parameters
- Coding and testing
Packaged with the stand-alone
1. Create a Maven project, build the project structure
- New Maven project, such as: com.itlaoqi.myspringboot
- Creating resources / templates directory to store the template engine
- Creating resources / static directory, store static pages
Creating resources / application.properties, SPB core profile
2. Open pom.xml, introducing dependent
- Basic spring-boot-starter-parent Spring Boot all the components references
- spring-boot-starter-web provides support for Web applications SB
- spring-boot-starter-thymeleaf support thymeleaf template engine
spring-boot-maven-plugin package provides application functionality SPB
<?xml version="1.0" encoding="UTF-8"?>
<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.qiyi</groupId>
<artifactId>myspringboot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. Open application.properties, configure Tomcat port
server.port=80
4. Create a test Controller, verify successful configuration
package com.qiyi.myspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping("/out") //绑定到out地址
@ResponseBody //直接向浏览器输出
public String out(){
return "success";
}
}
5. Create entry application class SpringBoot
package com.qiyi.myspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//告诉SpringBoot我是一个入口类,运行我就能启动SB
//会自动扫描可以被注入的类,并初始化
//@Repository / @Service / @Controller / @Component / @Entity
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
//启动SpringBoot,并初始化相关的组件
SpringApplication.run(MySpringBootApplication.class);
}
}