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}
想要項目中可以讀取配置文件中的信息需要配置插件
<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、測試test和生產(chǎn)product。
項目目錄構(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接口所在包的日志級別改成Debug,可以在控制臺打印sql。 spring: # 數(shù)據(jù)庫驅(qū)動 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;時區(qū)serverTimezone世界標(biāo)準(zhǔn)時間;服務(wù)器端身份校驗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ù) minIdle: 50 # 最小連接池數(shù)量 maxActive: 100 # 最大連接池數(shù)量 maxWait: 60000 # 從連接池中獲取連接的最大等待時間,單位毫秒。默認(rèn)-1,即不超時。配置了maxWait之后,缺省啟用公平鎖,并發(fā)效率會有所下降 timeBetweenEvictionRunsMillis: 60000 #單位毫秒 minEvictableIdleTimeMillis: 300000 # 配置一個連接在池中最小生存的時間,單位是毫秒 validationQuery: select 1 # 驗證sql是否可用,每中數(shù)據(jù)庫的配置值都不同 testWhileIdle: true # 建議配置為true,不影響性能,并且保證安全性。申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測連接是否有效。 testOnBorrow: false # 申請連接時執(zhí)行validationQuery檢測連接是否有效,做了這個配置會降低性能。 testOnReturn: false # 歸還連接時執(zhí)行validationQuery檢測連接是否有效,做了這個配置會降低性能 # 打開PSCache,并且指定每個連接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall,slf4j #監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sal無法統(tǒng)計,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白名單,未配置則只能在本地訪問,多個的華是127.0.0.1,192.168.10.100 reset-enable: false # 開啟 Reset All 功能 reset-enable 屬性即使設(shè)置為 false,重置按鈕也會顯示,只是點擊該按鈕并不會重置而已 login-username: admin # 設(shè)置登錄用戶名 login-password: admin # 設(shè)置登錄密碼 # allow=127.0.0.1 # 白名單(如果allow沒有配置或者為空,則允許所有訪問) # deny= # 黑名單(deny 優(yōu)先于 allow,如果在 deny 列表中,就算在 allow 列表中,也會被拒絕) # mybatis配置 mybatis: mapper-locations: classpath:mapper/*.xml # 標(biāo)注待解析的mapper的xml文件位置 type-aliases-package: com.example.demo.domain # 標(biāo)注實體類位置 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 指定mybatis所用日志的具體實現(xiàn),未指定時將自動查找 map-underscore-to-camel-case: true # 開啟自動駝峰命名規(guī)則(camel case)映射 lazy-loading-enabled: true # 開啟延時加載開關(guān) aggressive-lazy-loading: false # 將積極加載改為消極加載(即按需加載),默認(rèn)值是false lazy-load-trigger-methods: "" # 阻擋不相干的操作觸發(fā),實現(xiàn)懶加載 cache-enabled: true # 打開全局緩存開關(guān)(二級環(huán)境),默認(rèn)值是true # MyBatis使用pageHelper分頁 pagehelp: helper-dialect: mysql # 配置使用哪種數(shù)據(jù)庫語言,不配置的話pageHelper也會自動檢測 reasonable: true # 在啟用合理化時,如果pageNum<1,則會查詢第一頁,如果pageNum>pages 則會查詢最后一頁 support-methods-arguments: true # 支持通過Mapper接口參數(shù)來傳遞分頁參數(shù),默認(rèn)值為false,分頁插件會從查詢方法的參數(shù)值中,自動根據(jù)上面的param params: count=countSql mybatis-plus: configuration: #控制臺打印完整帶參數(shù)SQL語句 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl call-setters-on-nulls: true # 這里根據(jù)自己項目的包修改,掃描到自己的*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é)點mybatis的mapper.xml文件都會被漏掉。 --> <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里的項目配置文件 --> <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><!--編譯跳過測試文件檢查的生命周期--> <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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
全網(wǎng)最全最細(xì)的jmeter接口測試教程以及接口測試流程(入門教程)
本文主要介紹了全網(wǎng)最全最細(xì)的jmeter接口測試教程以及接口測試流程,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11教你利用JAVA實現(xiàn)可以自行關(guān)閉服務(wù)器的方法
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著利用JAVA實現(xiàn)可以自行關(guān)閉服務(wù)器的方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06IDEA中application.properties的圖標(biāo)顯示不正常的問題及解決方法
這篇文章主要介紹了IDEA中application.properties的圖標(biāo)顯示不正常的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04SpringBoot整合Quartz實現(xiàn)動態(tài)配置的代碼示例
這篇文章將介紹如何把Quartz定時任務(wù)做成接口,實現(xiàn)以下功能的動態(tài)配置添加任務(wù),修改任務(wù),暫停任務(wù),恢復(fù)任務(wù),刪除任務(wù),任務(wù)列表,任務(wù)詳情,文章通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07SpringBoot中使用@scheduled定時執(zhí)行任務(wù)的坑
本文主要介紹了SpringBoot中使用@scheduled定時執(zhí)行任務(wù)的坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05