【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.然后執行一下就好了。

 

      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM