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

首頁 > 開發 > Java > 正文

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

2024-07-13 10:15:25
字體:
來源:轉載
供稿:網友

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

廢話不多說,關于spring-boot是什么具體請百度。

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

1. spring-boot是一個mavan項目,所以其使用的jar包全部是通過maven管理,當然,使用maven也是非常方便的。

首先上我的項目目錄結構:

spring,boot微框架,springboot框架搭建,boot框架      

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>

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

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

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

通過一個啟動文件的main方法

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

先來解釋下這個文件中的代碼。

@Configuration:標注此文件為一個配置項

@EnableAutoConfiguration:使用自動配置

@ComponentScan:可掃描的

SpringApplication:啟動管理器。

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 法库县| 临安市| 鹤壁市| 洛浦县| 淳化县| 蓬溪县| 南京市| 盘锦市| 揭阳市| 玉林市| 郸城县| 江川县| 灵璧县| 景德镇市| 临湘市| 平果县| 乌鲁木齐市| 桂阳县| 常山县| 敦煌市| 金寨县| 武穴市| 东乡县| 新建县| 遂宁市| 沐川县| 钟祥市| 六盘水市| 定州市| 邯郸县| 阜新| 大同县| 墨脱县| 古蔺县| 东台市| 高阳县| 太原市| 莎车县| 莎车县| 蒲江县| 远安县|