工具:IDEA intellj
SpringBoot在建立生产中的独立程序上非常简便、只需要一些简便的配置就能运行起来。大致有如下特点:
- 创建独立的Spring applications
- 能够使用内嵌的Tomcat, Jetty or Undertow,不需要部署war
- 提供starter pom来简化maven配置
- 自动配置Spring
- 提供一些生产环境的特性,比如metrics, health checks and externalized configuration
- 绝对没有代码生成和XML配置要求
SpringMVC处理Web请求,Thymeleaf定义Web视图,Spring Data JPA把阅读列表持久化到数据里,thymeleaf为springboot默认配置的模板引擎,默认的模板配置路径为:src/main/resources/templates
,通过这种方法用html文件来替代jsp,jsp无法实现thymeleaf的很多特性。
1,File ----new----spring initializr,填写好group和artifact
勾选web
勾选thymeleaf
完成项目创建。
目录如下:
快速开始:
先看一下pom.xml
<?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.cx</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这块统一用spring-boot-starter-parent做为parent.
插件是用来运行Springboot的,通常有两种方式可以运行SpringBoot,一种是通过直接运行main方法,另外一种是通过使用下面的插件运行。
两种方式有差别,一旦项目中需要访问资源的时候就需要通过插件运行,否则无法访问到资源.
整个程序的入口是SpringbootdemoApplication.java:
package com.cx.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
只要运行main方法或者插件,就能够正常访问了。
配置详解
@SpringBootApplication
这个注解是一个组合注解,聚合了多个注解的功能。
application.properties 文件详解
整个应用程序的配置文件,SpringBoot自动加载,SpringBoot提供针对各种组件的都可以通过application.properties进行配置,后续再进行详解。