App下載

總結(jié)配置SpringBoot環(huán)境的詳細(xì)知識內(nèi)容

猿友 2021-07-26 13:57:08 瀏覽數(shù) (1632)
反饋

一、pom文件配置

<!-- SpringBoot的父級依賴。只有設(shè)置了parent標(biāo)簽,項目才是SpringBoot項目  -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.2.RELEASE</version>
</parent>

<dependencies>
   <!-- web啟動器,加載web環(huán)境所需要的依賴 -->
   <dependency>
   		<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- mybatis 集成 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
    <!-- springboot 分頁插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.13</version>
    </dependency>
    <!-- mysql 驅(qū)動 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- c3p0 數(shù)據(jù)源 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.5</version>
    </dependency>
    <!-- Freemarker Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <!-- Thymeleaf Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- JavaMail -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <!-- AOP -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!-- Log -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <!-- tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- 為使用StringUtils工具類 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <!-- API文檔 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- DevTools 的坐標(biāo) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <!--當(dāng)前這個項目被繼承之后,這個不向下傳遞-->
        <optional>true</optional>	
    </dependency>
    <!-- SpringBoot單元測試 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!-- Ehcache -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    
</dependencies>

<!-- SpringBoot項目的打包插件 -->
<build>
    <finalName>springboot</finalName>
	<plugins>
  	<plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <fork>true</fork><!-- 如果沒有該配置,熱部署的devtools不生效 -->
        </configuration>
    </plugin>
  </plugins>
</build>

相關(guān)starter系列坐標(biāo)參考:

名稱 描述
spring-boot-starter 核心Spring Boot starter,包括自動配置支持,日志和YAML
spring-boot-starter-actuator 生產(chǎn)準(zhǔn)備的特性,用于幫我們監(jiān)控和管理應(yīng)用
spring-boot-starter-amqp 對”高級消息隊列協(xié)議”的支持,通過spring-rabbit實現(xiàn)
spring-boot-starter-aop 對面向切面編程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch 對Spring Batch的支持,包括HSQLDB數(shù)據(jù)庫
spring-boot-starter-cloud-connectors 對Spring Cloud Connectors的支持,簡化在云平臺下(例如,Cloud Foundry 和Heroku)服務(wù)的連接
spring-boot-starter-data-elasticsearch 對Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire 對GemFire分布式數(shù)據(jù)存儲的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa 對”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb 對MongoDB NOSQL數(shù)據(jù)庫的支持,包括spring-data-mongodb
spring-boot-starter-data-rest 對通過REST暴露Spring Data倉庫的支持,通過spring-data-rest-webmvc實現(xiàn)
spring-boot-starter-data-solr 對Apache Solr搜索平臺的支持,包括spring-data-solr
spring-boot-starter-freemarker 對FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates 對Groovy模板引擎的支持
spring-boot-starter-hateoas 對基于HATEOAS的RESTful服務(wù)的支持,通過spring-hateoas實現(xiàn)
spring-boot-starter-hornetq 對”Java消息服務(wù)API”的支持,通過HornetQ實現(xiàn)
spring-boot-starter-integration 對普通spring-integration模塊的支持
spring-boot-starter-jdbc 對JDBC數(shù)據(jù)庫的支持
spring-boot-starter-jersey 對Jersey RESTful Web服務(wù)框架的支持
spring-boot-starter-jta-atomikos 對JTA分布式事務(wù)的支持,通過Atomikos實現(xiàn)
spring-boot-starter-jta-bitronix 對JTA分布式事務(wù)的支持,通過Bitronix實現(xiàn)
spring-boot-starter-mail 對javax.mail的支持
spring-boot-starter-mobile 對spring-mobile的支持
spring-boot-starter-mustache 對Mustache模板引擎的支持
spring-boot-starter-redis 對REDIS鍵值數(shù)據(jù)存儲的支持,包括spring-redis
spring-boot-starter-security 對spring-security的支持
spring-boot-starter-social-facebook 對spring-social-facebook的支持
spring-boot-starter-social-linkedin 對spring-social-linkedin的支持
spring-boot-starter-social-twitter 對spring-social-twitter的支持
spring-boot-starter-test 對常用測試依賴的支持,包括JUnit, Hamcrest和Mockito,還有spring-test模塊
spring-boot-starter-thymeleaf 對Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity 對Velocity模板引擎的支持
spring-boot-starter-web 對全棧web開發(fā)的支持, 包括Tomcat和spring-webmvc
spring-boot-starter-websocket 對WebSocket開發(fā)的支持
spring-boot-starter-ws 對Spring Web服務(wù)的支持

二、yml文件配置

yml文件放在resources目錄下

## 端口號  上下文路徑
server:
  port: 8989
  servlet:
    context-path: /mvc

## 數(shù)據(jù)源配置
spring:
  datasource:
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/hr
    username: root
    password: root
    
  freemarker:
    suffix: .ftl
    content-type: text/html  #響應(yīng)格式
    charset: UTF-8
    template-loader-path: classpath:/views/
    
  ## 熱部署配置
  devtools:
    restart:
      enabled: true
      # 設(shè)置重啟的目錄,添加目錄的文件需要restart
      additional-paths: src/main/java
      # 解決項目自動重新編譯后接口報404的問題
      poll-interval: 3000
      quiet-period: 1000
      
  ## Ehcache緩存配置
  cache:
    ehcache:
      config: classpath:ehcache.xml
    
## mybatis 配置
mybatis:
  #映射文件的存放路徑
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.xxxx.springboot.po
  configuration:
    ## 下劃線轉(zhuǎn)駝峰配置
    map-underscore-to-camel-case: true

## pageHelper
pagehelper:
  helper-dialect: mysql

## 顯示dao 執(zhí)行sql語句
logging:
  level:
    com:
      xxxx:
        dao: debug

三、SQL映射文件配置

sql映射文件路徑在"resources/mappers/"下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xxxx.dao.UserMapper">
    
</mapper>

四、ehcahe.xml 文件

配置在resources下

<ehcache name="mycache">
  	<!-- 
		如果不使用磁盤存儲,只需要將diskStore注釋掉即可;
		如果使用,需要在ehcache.xml文件中的ehcahce元素下的定義一個diskStore元素并指定其path屬性。 
	-->
    <diskStore path="C:javacache"/>
    <!--
        name:緩存名稱。
        maxElementsInMemory:緩存最大數(shù)目
        maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。
        eternal:對象是否永久有效,一但設(shè)置了,timeout將不起作用。
        overflowToDisk:是否保存到磁盤,當(dāng)系統(tǒng)宕機時
        timeToIdleSeconds:設(shè)置對象在失效前的允許閑置時間(單位:秒)。
        	僅當(dāng)eternal=false對象不是永久有效時使用,可選屬性,默認(rèn)值是0,表示可閑置時間無窮大。
        timeToLiveSeconds:設(shè)置對象在失效前允許存活時間(單位:秒)。
            最大時間介于創(chuàng)建時間和失效時間之間。
			僅當(dāng)eternal=false對象不是永久有效時使用,默認(rèn)是0,也就是對象存活時間無窮大。
        diskPersistent:是否緩存虛擬機重啟期數(shù)據(jù) 
			Whether the disk store persists between restarts of the Virtual Machine. 			 The default value is false.
        diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。
			默認(rèn)是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū)。
        diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認(rèn)是120秒。
        memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時,會根據(jù)指定的策略去清理內(nèi)存
             默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進先出)或是LFU(較少使用)。
        clearOnFlush:內(nèi)存數(shù)量最大時是否清除。
        memoryStoreEvictionPolicy:
			可選策略有:
				LRU(最近最少使用,默認(rèn)策略)
					Less Frequently Used,就是例子中使用的策略,就是一直以來最少被使用的。
				FIFO(先進先出)
					first in first out,這個是大家最熟的,先進先出。
				LFU(最少訪問次數(shù))
					Least Recently Used,最近最少使用的。
					緩存的元素有一個時間戳,當(dāng)緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,
					那么現(xiàn)有緩存元素中時間戳離當(dāng)前時間最遠(yuǎn)的元素將被清出緩存。
        -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <cache
            name="users"
            eternal="false"
            maxElementsInMemory="100"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="300"
            memoryStoreEvictionPolicy="LRU"/>
</ehcache>

到此本篇關(guān)于 SpringBoot 環(huán)境配置知識總結(jié)的文章就介紹到這了,想要了解更多相關(guān) SpringBoot 其他內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持我們!

0 人點贊