spring-boot是一個(gè)快速構(gòu)建環(huán)境的一套框架,其設(shè)計(jì)理念是盡可能的減少xml的配置,用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置。
廢話不多說(shuō),關(guān)于spring-boot是什么具體請(qǐng)百度。
官網(wǎng):http://projects.spring.io/spring-boot
1. spring-boot是一個(gè)mavan項(xiàng)目,所以其使用的jar包全部是通過(guò)maven管理,當(dāng)然,使用maven也是非常方便的。
首先上我的項(xiàng)目目錄結(jié)構(gòu):

spring-boot打出來(lái)的包是一個(gè)可執(zhí)行jar包的狀態(tài),使用的是內(nèi)置的tomcat服務(wù)器,所以不需要將項(xiàng)目轉(zhuǎn)成EJB項(xiàng)目。
2.設(shè)置pom.xml文件
使用過(guò)maven的朋友都知道,maven通過(guò)pom文件的依賴來(lái)進(jìn)行管理jar包,所以核心也是這個(gè)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.lclc.boot</groupId> <artifactId>boot-cache</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <!--Spring Boot基礎(chǔ)父類,其中包含了很多必要的jar包,如果不使用父類,則需要自己去依賴這些jars --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.3.RELEASE</version> </parent> <dependencies> <!-- web程序的啟動(dòng)項(xiàng)依賴,通過(guò)此依賴可引入內(nèi)嵌的tomcat等web必須的jars --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring-data-jpa程序的啟動(dòng)項(xiàng)依賴,底層為hibernate實(shí)現(xiàn),若不使用此框架則可以依賴其他的orm框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- thymeleaf程序的啟動(dòng)項(xiàng)依賴,spring-boot對(duì)thymeleaf模板引擎支持最好,建議模板引擎使用此框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- MySQL依賴,使用spring-data-jpa需要指定一個(gè)數(shù)據(jù)庫(kù)方言,用于連接數(shù)據(jù)庫(kù),即mysql驅(qū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> </dependencies> </dependencyManagement> <build> <plugins> <!-- 通過(guò)maven構(gòu)建的插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 倉(cāng)庫(kù),使用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>pom.xml
3.使用maven update 下載jar包
4.由于我們使用了thymeleaf引擎,此引擎需要一個(gè)templates文件夾來(lái)存放靜態(tài)頁(yè)面,以便進(jìn)行跳轉(zhuǎn)前臺(tái)。
所以在resources下添加此文件夾并加入一個(gè)默認(rèn)的頁(yè)面index.html(注:此文件夾下必須有一個(gè)html頁(yè)面,否則thymeleaf啟動(dòng)項(xiàng)會(huì)拋異常)
5.編寫(xiě)application.properties
這個(gè)配置文件是對(duì)spring-boot的一些配置,spring-boot通過(guò)此文件對(duì)集成在其中的一些框架進(jìn)行配置。由我的項(xiàng)目結(jié)構(gòu)可以看出,我有兩個(gè)application.properties文件:
application.properties:主配置文件,spring-boot直接讀取這個(gè)文件。注:配置文件必須放在resources下,即放在項(xiàng)目根目錄下。
application-dev.properties:開(kāi)發(fā)環(huán)境配置文件,這個(gè)是我的開(kāi)發(fā)環(huán)境的配置文件,為了簡(jiǎn)化一些開(kāi)發(fā),所以需要一些與部署環(huán)境不同的配置,比如頁(yè)面緩存之類的。此文件通過(guò)application.properties的spring.profiles.active屬性進(jìn)行配置讀取。
上兩個(gè)文件的代碼:
首先是application.properties:

# PROFILES## dev | prod | testspring.profiles.active=dev# EMBEDDED SERVER CONFIGURATION (ServerProperties)server.port=8080server.session-timeout=30server.context-path=server.tomcat.max-threads=0server.tomcat.uri-encoding=UTF-8# THYMELEAF (ThymeleafAutoConfiguration)spring.thymeleaf.encoding=UTF-8# DataSourcespring.datasource.initialize=falsespring.datasource.test-on-borrow=falsespring.datasource.test-on-return=falsespring.datasource.test-while-idle=truespring.datasource.max-wait-millis=30000spring.datasource.validation-query=SELECT 1spring.datasource.time-between-eviction-runs-millis=20000spring.datasource.min-evictable-idle-time-millis=28700application.properties
然后是application-dev.properties:

#page cachespring.thymeleaf.cache=false# DATASOURCE spring.datasource.platform=mysqlspring.datasource.url=jdbc:mysql://localhost/test_development?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=truespring.datasource.username=rootspring.datasource.passWord=123456spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.max-active=5spring.datasource.max-idle=2spring.datasource.min-idle=1spring.datasource.initial-size=1spring.datasource.initialize=false# JPaspring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=falsespring.jpa.properties.hibernate.use_sql_comments=trueapplication-dev.properties
6.于是配置便完成了,現(xiàn)在看怎么使用spring-boot進(jìn)行啟動(dòng)一個(gè)web程序
spring-boot打的包是一個(gè)可執(zhí)行的jar包,當(dāng)然也可以打成可執(zhí)行的war包,啟動(dòng)服務(wù)器就完全不需要像以前一樣弄一個(gè)tomcat進(jìn)行啟動(dòng)了,完全是java application進(jìn)行啟動(dòng)
通過(guò)一個(gè)啟動(dòng)文件的main方法

@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application { public static void main(String[] args){ SpringApplication springApplication = new SpringApplication (Application.class); springApplication.run (args); }}Application.java先來(lái)解釋下這個(gè)文件中的代碼。
@Configuration:標(biāo)注此文件為一個(gè)配置項(xiàng)
@EnableAutoConfiguration:使用自動(dòng)配置
@ComponentScan:可掃描的
SpringApplication:?jiǎn)?dòng)管理器。
注意,由于是使用注解的方式,所以需要配置掃描路徑,spring-boot使用的是啟動(dòng)管理器所在的包為根掃描路徑。會(huì)掃描其所在的包和子包,所以需要將Application.java放在跟路徑下,即com.test這個(gè)包里。
7.然后執(zhí)行一下就好了。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注