spring-boot是一個快速構建環境的一套框架,其設計理念是盡可能的減少xml的配置,用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。
廢話不多說,關于spring-boot是什么具體請百度。
官網:http://projects.spring.io/spring-boot
1. spring-boot是一個mavan項目,所以其使用的jar包全部是通過maven管理,當然,使用maven也是非常方便的。
首先上我的項目目錄結構:
spring-boot打出來的包是一個可執行jar包的狀態,使用的是內置的tomcat服務器,所以不需要將項目轉成EJB項目。
2.設置pom.xml文件
使用過maven的朋友都知道,maven通過pom文件的依賴來進行管理jar包,所以核心也是這個pom.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
<? 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.lclc.boot</ groupId > < artifactId >boot-cache</ artifactId > < version >0.0.1-SNAPSHOT</ version > <!-- Inherit defaults from Spring Boot --> < parent > <!--Spring Boot基礎父類,其中包含了很多必要的jar包,如果不使用父類,則需要自己去依賴這些jars --> < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.1.3.RELEASE</ version > </ parent > < dependencies > <!-- web程序的啟動項依賴,通過此依賴可引入內嵌的tomcat等web必須的jars --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!-- spring-data-jpa程序的啟動項依賴,底層為hibernate實現,若不使用此框架則可以依賴其他的orm框架 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jpa</ artifactId > </ dependency > <!-- thymeleaf程序的啟動項依賴,spring-boot對thymeleaf模板引擎支持最好,建議模板引擎使用此框架 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-thymeleaf</ artifactId > </ dependency > <!-- mysql依賴,使用spring-data-jpa需要指定一個數據庫方言,用于連接數據庫,即mysql驅動 --> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > </ dependencies > < dependencyManagement > < dependencies > </ dependencies > </ dependencyManagement > < build > < plugins > <!-- 通過maven構建的插件 --> < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > <!-- 倉庫,使用spring-boot RELEASE版本需要這些 --> < repositories > < repository > < id >spring-snapshots</ id > < url >http://repo.spring.io/snapshot</ url > < snapshots > < enabled >true</ enabled > </ snapshots > </ repository > < repository > < id >spring-milestones</ id > < url >http://repo.spring.io/milestone</ url > </ repository > </ repositories > < pluginRepositories > < pluginRepository > < id >spring-snapshots</ id > < url >http://repo.spring.io/snapshot</ url > </ pluginRepository > < pluginRepository > < id >spring-milestones</ id > < url >http://repo.spring.io/milestone</ url > </ pluginRepository > </ pluginRepositories > </ project > |
3.使用maven update 下載jar包
4.由于我們使用了thymeleaf引擎,此引擎需要一個templates文件夾來存放靜態頁面,以便進行跳轉前臺。
所以在resources下添加此文件夾并加入一個默認的頁面index.html(注:此文件夾下必須有一個html頁面,否則thymeleaf啟動項會拋異常)
5.編寫application.properties
這個配置文件是對spring-boot的一些配置,spring-boot通過此文件對集成在其中的一些框架進行配置。由我的項目結構可以看出,我有兩個application.properties文件:
application.properties:主配置文件,spring-boot直接讀取這個文件。注:配置文件必須放在resources下,即放在項目根目錄下。
application-dev.properties:開發環境配置文件,這個是我的開發環境的配置文件,為了簡化一些開發,所以需要一些與部署環境不同的配置,比如頁面緩存之類的。此文件通過application.properties的spring.profiles.active屬性進行配置讀取。
上兩個文件的代碼:
首先是application.properties:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# PROFILES ## dev | prod | test spring.profiles.active=dev # EMBEDDED SERVER CONFIGURATION (ServerProperties) server.port=8080 server.session-timeout=30 server.context-path= server.tomcat.max-threads=0 server.tomcat.uri-encoding=UTF-8 # THYMELEAF (ThymeleafAutoConfiguration) spring.thymeleaf.encoding=UTF-8 # DataSource spring.datasource.initialize=false spring.datasource.test-on-borrow=false spring.datasource.test-on-return=false spring.datasource.test-while-idle=true spring.datasource.max-wait-millis=30000 spring.datasource.validation-query=SELECT 1 spring.datasource.time-between-eviction-runs-millis=20000 spring.datasource.min-evictable-idle-time-millis=28700 |
然后是application-dev.properties:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#page cache spring.thymeleaf.cache=false # DATASOURCE spring.datasource.platform=mysql spring.datasource.url=jdbc:mysql://localhost/test_development?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.max-active=5 spring.datasource.max-idle=2 spring.datasource.min-idle=1 spring.datasource.initial-size=1 spring.datasource.initialize=false # JPA spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=false spring.jpa.properties.hibernate.use_sql_comments=true |
6.于是配置便完成了,現在看怎么使用spring-boot進行啟動一個web程序
spring-boot打的包是一個可執行的jar包,當然也可以打成可執行的war包,啟動服務器就完全不需要像以前一樣弄一個tomcat進行啟動了,完全是java application進行啟動
通過一個啟動文件的main方法
1
2
3
4
5
6
7
8
9
|
@Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args){ SpringApplication springApplication = new SpringApplication (Application. class ); springApplication.run (args); } } |
先來解釋下這個文件中的代碼。
@Configuration:標注此文件為一個配置項
@EnableAutoConfiguration:使用自動配置
@ComponentScan:可掃描的
SpringApplication:啟動管理器。
注意,由于是使用注解的方式,所以需要配置掃描路徑,spring-boot使用的是啟動管理器所在的包為根掃描路徑。會掃描其所在的包和子包,所以需要將Application.java放在跟路徑下,即com.test這個包里。
7.然后執行一下就好了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/lic309/p/4073307.html