1. 程式人生 > >【spring-boot】快速構建spring-boot微框架

【spring-boot】快速構建spring-boot微框架

  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檔案

<?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>
pom.xml

  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:

# 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.properties

               然後是application-dev.properties:

#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
application-dev.properties

  6.於是配置便完成了,現在看怎麼使用spring-boot進行啟動一個web程式

    spring-boot打的包是一個可執行的jar包,當然也可以打成可執行的war包,啟動伺服器就完全不需要像以前一樣弄一個tomcat進行啟動了,完全是java application進行啟動

    通過一個啟動檔案的main方法

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args){
        SpringApplication springApplication = new SpringApplication (Application.class);
        springApplication.run (args);
    }
}
Application.java

    先來解釋下這個檔案中的程式碼。

  @Configuration:標註此檔案為一個配置項

  @EnableAutoConfiguration:使用自動配置

  @ComponentScan:可掃描的

  SpringApplication:啟動管理器。

  注意,由於是使用註解的方式,所以需要配置掃描路徑,spring-boot使用的是啟動管理器所在的包為根掃描路徑。會掃描其所在的包和子包,所以需要將Application.java放在跟路徑下,即com.test這個包裡。

  7.然後執行一下就好了。