国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

【spring-boot】快速構(gòu)建spring-boot微框架

2019-11-14 21:44:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
【sPRing-boot】快速構(gòu)建spring-boot微框架

  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=28700
application.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=true
application-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í)行一下就好了。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江西省| 湘潭市| 房产| 咸丰县| 女性| 德阳市| 海宁市| 霍州市| 呼玛县| 牡丹江市| 黎平县| 华宁县| 白沙| 涿鹿县| 邵东县| 灵宝市| 屏边| 都兰县| 通州区| 河西区| 余江县| 彝良县| 崇明县| 井冈山市| 古浪县| 会东县| 新平| 招远市| 广宁县| 绥江县| 大方县| 习水县| 龙游县| 崇义县| 常山县| 饶河县| 岑巩县| 维西| 绥芬河市| 浑源县| 寻乌县|