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

  spring-boot是一個快速構建環境的一套框架,其設計理念是儘量的減小xml的配置,用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員再也不須要定義樣板化的配置。html

  廢話很少說,關於spring-boot是什麼具體請百度。java

  官網:http://projects.spring.io/spring-bootmysql

  1. spring-boot是一個mavan項目,因此其使用的jar包所有是經過maven管理,固然,使用maven也是很是方便的。web

    首先上個人項目目錄結構:spring

      

    spring-boot打出來的包是一個可執行jar包的狀態,使用的是內置的tomcat服務器,因此不須要將項目轉成EJB項目。sql

  2.設置pom.xml文件數據庫

    使用過maven的朋友都知道,maven經過pom文件的依賴來進行管理jar包,因此核心也是這個pom.xml文件apache

    

<?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文件夾來存放靜態頁面,以便進行跳轉前臺。tomcat

    因此在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.而後執行一下就行了。

相關文章
相關標籤/搜索