application.yml的格式寫法和pom.xml讀取配置插件方式
application.yml格式寫法和pom.xml讀取配置插件
application.yml的格式寫法:(注意在值的前面有空格)
server:
? ? port: 8081
aa:
? ? name: 代斌
? ? age: 21
? ? list: [你好, 哈利]
? ? map: {a: 世界,b: 不知道}
? ? dog: {dog_name: 狗名字, dog_age: 89}想要項(xiàng)目中可以讀取配置文件中的信息需要配置插件
<dependency> ? ?<groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-configuration-processor</artifactId> ? ? <optional>true</optional> </dependency>
用法
**Aa類**
package com.example.daibin.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "aa")
public class Aa {
? ? private String name;
? ? private int age;
? ? private List<Object> list;
? ? private Map<String,Object> map;
? ? private Dog dog;
? ? @Override
? ? public String toString() {
? ? ? ? return "Aa{" +
? ? ? ? ? ? ? ? "name='" + name + '\'' +
? ? ? ? ? ? ? ? ", age=" + age +
? ? ? ? ? ? ? ? ", list=" + list +
? ? ? ? ? ? ? ? ", map=" + map +
? ? ? ? ? ? ? ? ", dog=" + dog +
? ? ? ? ? ? ? ? '}';
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
? ? public int getAge() {
? ? ? ? return age;
? ? }
? ? public void setAge(int age) {
? ? ? ? this.age = age;
? ? }
? ? public List<Object> getList() {
? ? ? ? return list;
? ? }
? ? public void setList(List<Object> list) {
? ? ? ? this.list = list;
? ? }
? ? public Map<String, Object> getMap() {
? ? ? ? return map;
? ? }
? ? public void setMap(Map<String, Object> map) {
? ? ? ? this.map = map;
? ? }
? ? public Dog getDog() {
? ? ? ? return dog;
? ? }
? ? public void setDog(Dog dog) {
? ? ? ? this.dog = dog;
? ? }
}**dog類**
package com.example.daibin.bean;
public class Dog {
? ? private String dog_name;
? ? private int dog_age;
? ? @Override
? ? public String toString() {
? ? ? ? return "Dog{" +
? ? ? ? ? ? ? ? "dog_name='" + dog_name + '\'' +
? ? ? ? ? ? ? ? ", dog_age=" + dog_age +
? ? ? ? ? ? ? ? '}';
? ? }
? ? public String getDog_name() {
? ? ? ? return dog_name;
? ? }
? ? public void setDog_name(String dog_name) {
? ? ? ? this.dog_name = dog_name;
? ? }
? ? public int getDog_age() {
? ? ? ? return dog_age;
? ? }
? ? public void setDog_age(int dog_age) {
? ? ? ? this.dog_age = dog_age;
? ? }
}這是輸出的結(jié)果(說明讀取配置文件中的信息成功)
Aa{name=’代斌’, age=21, list=[你好, 哈利], map={a=世界, b=不知道}, dog=Dog{dog_name=’狗名字’, dog_age=89}}
SpringBoot配置文件(application.properties、application.yml與pom.xml)
配置文件–application.properties與application.yml
老式配置文件是application.properties,新式配置文件是application.yml。
不同的配置文件可以用于不同的用處,依次是開發(fā)dev、測(cè)試test和生產(chǎn)product。

項(xiàng)目目錄構(gòu)建如下:

在新配置文件中application.yml(位于config目錄下 )中指定生效哪種類型的配置,如dev、test和product。
spring:
profiles:
active: dev配置文件application-dev.yml
的具體內(nèi)容可根據(jù)需要從網(wǎng)上摘取,示例僅供參考。
#服務(wù)端口
server:
port: 8080
#日志
logging:
level:
com.example.demo.mapper: debug # 將mapper接口所在包的日志級(jí)別改成Debug,可以在控制臺(tái)打印sql。
spring:
# 數(shù)據(jù)庫驅(qū)動(dòng)
datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mytest?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 #數(shù)據(jù)庫mytest;時(shí)區(qū)serverTimezone世界標(biāo)準(zhǔn)時(shí)間;服務(wù)器端身份校驗(yàn)useSSL
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# Druid的配置(數(shù)據(jù)庫連接池除了druid外,還有DBCP和C3PO,druid是阿里巴巴開發(fā)的,為監(jiān)控而生的數(shù)據(jù)庫連接池,性能強(qiáng)大,能夠通過頁面分析sql的性能)
druid:
initial-size: 50 # 初始化時(shí)建立物理連接的個(gè)數(shù)
minIdle: 50 # 最小連接池?cái)?shù)量
maxActive: 100 # 最大連接池?cái)?shù)量
maxWait: 60000 # 從連接池中獲取連接的最大等待時(shí)間,單位毫秒。默認(rèn)-1,即不超時(shí)。配置了maxWait之后,缺省啟用公平鎖,并發(fā)效率會(huì)有所下降
timeBetweenEvictionRunsMillis: 60000 #單位毫秒
minEvictableIdleTimeMillis: 300000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
validationQuery: select 1 # 驗(yàn)證sql是否可用,每中數(shù)據(jù)庫的配置值都不同
testWhileIdle: true # 建議配置為true,不影響性能,并且保證安全性。申請(qǐng)連接的時(shí)候檢測(cè),如果空閑時(shí)間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測(cè)連接是否有效。
testOnBorrow: false # 申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,做了這個(gè)配置會(huì)降低性能。
testOnReturn: false # 歸還連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,做了這個(gè)配置會(huì)降低性能
# 打開PSCache,并且指定每個(gè)連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j #監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sal無法統(tǒng)計(jì),wall用于防火墻
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
# 配置 WebStatFilter,WebStatFilter 用于采集 web-jdbc 關(guān)聯(lián)監(jiān)控的數(shù)據(jù):
web-stat-filter:
enabled: true # 啟用 WebStatFilter
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# StatViewServlet 的配置
stat-view-servlet:
url-pattern: "/druid/*" # 內(nèi)置監(jiān)控頁面的地址
enabled: true # 啟用內(nèi)置的監(jiān)控頁面
allow: 127.0.0.1 # IP白名單,未配置則只能在本地訪問,多個(gè)的華是127.0.0.1,192.168.10.100
reset-enable: false # 開啟 Reset All 功能 reset-enable 屬性即使設(shè)置為 false,重置按鈕也會(huì)顯示,只是點(diǎn)擊該按鈕并不會(huì)重置而已
login-username: admin # 設(shè)置登錄用戶名
login-password: admin # 設(shè)置登錄密碼
# allow=127.0.0.1 # 白名單(如果allow沒有配置或者為空,則允許所有訪問)
# deny= # 黑名單(deny 優(yōu)先于 allow,如果在 deny 列表中,就算在 allow 列表中,也會(huì)被拒絕)
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml # 標(biāo)注待解析的mapper的xml文件位置
type-aliases-package: com.example.demo.domain # 標(biāo)注實(shí)體類位置
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 指定mybatis所用日志的具體實(shí)現(xiàn),未指定時(shí)將自動(dòng)查找
map-underscore-to-camel-case: true # 開啟自動(dòng)駝峰命名規(guī)則(camel case)映射
lazy-loading-enabled: true # 開啟延時(shí)加載開關(guān)
aggressive-lazy-loading: false # 將積極加載改為消極加載(即按需加載),默認(rèn)值是false
lazy-load-trigger-methods: "" # 阻擋不相干的操作觸發(fā),實(shí)現(xiàn)懶加載
cache-enabled: true # 打開全局緩存開關(guān)(二級(jí)環(huán)境),默認(rèn)值是true
# MyBatis使用pageHelper分頁
pagehelp:
helper-dialect: mysql # 配置使用哪種數(shù)據(jù)庫語言,不配置的話pageHelper也會(huì)自動(dòng)檢測(cè)
reasonable: true # 在啟用合理化時(shí),如果pageNum<1,則會(huì)查詢第一頁,如果pageNum>pages 則會(huì)查詢最后一頁
support-methods-arguments: true # 支持通過Mapper接口參數(shù)來傳遞分頁參數(shù),默認(rèn)值為false,分頁插件會(huì)從查詢方法的參數(shù)值中,自動(dòng)根據(jù)上面的param
params: count=countSql
mybatis-plus:
configuration:
#控制臺(tái)打印完整帶參數(shù)SQL語句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
call-setters-on-nulls: true
# 這里根據(jù)自己項(xiàng)目的包修改,掃描到自己的*xml文件
# mapper-locations:
spring.thymeleaf.content-type: text/html
spring.thymeleaf.cache: false
spring.thymeleaf.mode: LEGACYHTML5配置文件–pom.xml
擴(kuò)展: 數(shù)據(jù)庫連接池—druid:阿里巴巴開發(fā)的,為監(jiān)控而生的數(shù)據(jù)庫連接池,其性能強(qiáng)大,能夠通過頁面分析sql的性能。
https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter 網(wǎng)站中可以查到最新的druid版本,然后可以在pom.xml中增加druid依賴。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--在pom.xml中添加druid的依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!--在pom.xml中添加fastjson的依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<!--指選擇大于1.2.78以上的最新版本(包括1.2.78版本)-->
<version>[1.2.78,)</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<!--MyBatis使用pageHelper分頁-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<!-- 如果不添加此節(jié)點(diǎn)mybatis的mapper.xml文件都會(huì)被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- 打包resource里的項(xiàng)目配置文件 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>static/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.7.RELEASE</version>
</plugin>
<plugin><!--編譯跳過測(cè)試文件檢查的生命周期-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot2.0實(shí)現(xiàn)優(yōu)雅停機(jī)的方法
這篇文章主要介紹了spring boot2.0實(shí)現(xiàn)優(yōu)雅停機(jī)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
java網(wǎng)絡(luò)編程之socket網(wǎng)絡(luò)編程示例(服務(wù)器端/客戶端)
這篇文章主要介紹了java socket網(wǎng)絡(luò)編程的示例,分為服務(wù)器端和客戶端,大家參考使用吧2014-01-01
Java的springcloud Sentinel是什么你知道嗎
這篇文章主要介紹了Java之springcloud Sentinel案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Java調(diào)用第三方http接口的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java調(diào)用第三方http接口的四種方式,在實(shí)際開發(fā)中我們經(jīng)常會(huì)與第三方公司進(jìn)行合作,接入第三方接口,文中給出了詳細(xì)的代碼實(shí)例,需要的朋友可以參考下2023-08-08
IntelliJ IDEA修改新建文件自動(dòng)生成注釋的user名
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA修改新建文件自動(dòng)生成注釋的user名,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼
這篇文章主要介紹了IDEA:Git stash 暫存分支修改的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
詳解SpringBoot Mongo 自增長ID有序規(guī)則
本文主要介紹springboot基于mongodb有序id生成,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實(shí)例講解
這篇文章主要介紹了Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實(shí)例,注意這里的雙端鏈表不是雙向鏈表,是在單鏈表的基礎(chǔ)上保存有對(duì)最后一個(gè)鏈接點(diǎn)的引用,需要的朋友可以參考下2016-04-04
Springboot集成Proguard生成混淆jar包方式
本文介紹了兩種Java代碼混淆工具:ClassFinal和ProGuard,ClassFinal是一個(gè)字節(jié)碼加密工具,但需要額外的加密包,使用復(fù)雜,ProGuard是一款開源的Java代碼混淆工具,可以有效地提高代碼的安全性,但對(duì)Spring框架的注解處理不夠完善2024-11-11

