一、概念和由來
1、什麼是 Spring Boot
Spring Boot 的設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用特定方式來進行配置,從而使開發人員不再需要定義樣板化的配置。
Spring Boot 其實不是什麼新的框架,它默認配置了很多框架的使用方式。
內置Tomcat和Jetty容器
Starter pom 簡化項目配置
大型項目的非功能特性,如:安全、指標、健康監測、外部配置等
沒有代碼生成和xml配置文件
2、內置 Servlet Container
tomcat8 + servelt規範3.1
jetty9.3+ servelt規範3.1
undertow1.3+ servelt規範3.1
3、開發調試工具
SpringBoot DevTools
二、創建 gradle 工程
1、創建 gradle 工程:http://start.spring.io/
你可以通過 Spring Initializr 來創建一個空的項目,也可以手動創建。
2、構建工程
要採用科學上網,否則會慢的讓你崩潰的!!
(1)修改gradle下載地址
(2)、使用阿里雲的maven庫
buildscript {
ext {
springBootVersion = '1.5.7.RELEASE'
}
repositories {
//mavenCentral()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
//maven { url "https://repo.spring.io/snapshot" }
//maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.tianhe.example.springboot'
version = '0.0.1-SNAPSHOT'
//生成的jar包包名和版本
jar {
baseName = 'HelloGradle'
version = '0.1.0'
}
//設置jdk的版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
//mavenCentral()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
//maven { url "https://repo.spring.io/snapshot" }
//maven { url "https://repo.spring.io/milestone" }
}
[compileJava,compileTestJava,javadoc]*.options*.encoding = "utf-8"
configurations.all {
exclude module: 'slf4j-jcl'
exclude module: 'slf4j-jdk14'
exclude module: 'slf4j-nop'
exclude module: 'slf4j-simple'
exclude module: 'slf4j-log4j12'
exclude module: 'log4j'
exclude module: 'commons-logging'
exclude module: 'commons-logging-api'
}
dependencies {
compile('org.slf4j:slf4j-api:1.7.15') {
force = true
}
compile('org.slf4j:jcl-over-slf4j:1.7.15') {
force = true
}
compile('org.slf4j:log4j-over-slf4j:1.7.15') {
force = true
}
compile('org.slf4j:jul-to-slf4j:1.7.15') {
force = true
}
compile('ch.qos.logback:logback-core:1.1.7') {
force = true
}
compile('ch.qos.logback:logback-classic:1.1.7') {
force = true
}
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile('com.fasterxml.jackson.core:jackson-databind:2.7.4')
compile('com.fasterxml.jackson.core:jackson-core:2.7.4')
compile('com.fasterxml.jackson.core:jackson-annotations:2.7.4')
compile('commons-httpclient:commons-httpclient:3.1')
compile('org.htmlparser:htmlparser:1.6')
compile "commons-lang:commons-lang:2.6"
compile "commons-io:commons-io:2.4"
compile "commons-codec:commons-codec:1.5"
runtime("mysql:mysql-connector-java")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
3、前端代碼
4、啟動應用
在IDE中直接直接執行main方法,然後訪問<code>http://localhost:8080/<code>即可。
三、附錄
1、thymeleaf Exception parsing document: template="xxxx"錯誤
Thymeleaf模板引擎,遇到不閉合標籤會報這個錯誤,很是蛋疼啊。最後發現是自動生成的meta標籤沒有關閉,太太坑了。
網上搜來的解決方案:
(1). 添加maven依賴
<dependency>
<groupid>net.sourceforge.nekohtml/<groupid>
<artifactid>nekohtml/<artifactid>
<version>1.9.22/<version>
(2). 更改application.properties屬性
spring.thymeleaf.mode=LEGACYHTML5
閱讀更多 歪脖攻城獅 的文章