成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Spring Boot 自定義配置文件(三)

caoym / 829人閱讀

摘要:警告此示例文件僅供參考。在自定義配置屬性添加自定義屬性在引用這個(gè)自定義屬性名為密碼為注意在獲取自定義屬性時(shí)一定要嚴(yán)格按照配置文件來(lái)獲取并且里面的字符串一定是花括號(hào)花括號(hào)里面的表示為里面自定義的字符串,所以本例就是表示為。

簡(jiǎn)介

上篇我們做了一些簡(jiǎn)單的運(yùn)行文件的配置,本篇帶領(lǐng)大家來(lái)認(rèn)識(shí)常用的一些配置,當(dāng)然了關(guān)于Spring Boot 這些配置太多太多了,如果想了解更多的話直接上官網(wǎng)參考一下,了解相關(guān)案例如本篇的配置。 application.properties配置官方指南參考。

Spring Boot有以下方式配置 一、application.properties配置

在 IntelliJ IDEA 開(kāi)發(fā)工具中創(chuàng)建項(xiàng)目的時(shí)候,默認(rèn)的配置文件是application.properties,接下來(lái)我們就學(xué)習(xí)一下然后配置一些我們?cè)陂_(kāi)發(fā)中經(jīng)常用到的配置項(xiàng),進(jìn)入帶領(lǐng)我們揭開(kāi)Spring Boot 項(xiàng)目的神秘的面紗。
在下面的文檔中我會(huì)在每個(gè)配置中進(jìn)行注解,這樣能更好的了解,當(dāng)然官方文檔有我們所需的全部配置,大家如果項(xiàng)目開(kāi)發(fā)中有需求那么可以進(jìn)入application.properties配置官方指南參考。

英: Appendix A. Common application properties

1. Various properties can be specified inside your application.properties file, inside your application.yml file, or as command line switches. 
   This appendix provides a list of common Spring Boot properties and references to the underlying classes that consume them.
2. [Note]
    Property contributions can come from additional jar files on your classpath, so you should not consider this an exhaustive list. 
    Also, you can define your own properties.
3. [Warning]
    This sample file is meant as a guide only. 
    Do not copy and paste the entire content into your application. Rather, pick only the properties that you need.

譯: 官方指南者三段話概況了今天我們要講得內(nèi)容:

1. 可以在application.properties文件中,application.yml文件中或命令行開(kāi)關(guān)中指定各種屬性。 本附錄提供了常用Spring Boot屬性的列表以及對(duì)使用它們的基礎(chǔ)類的引用。
2. [注意]
   屬性貢獻(xiàn)可以來(lái)自類路徑上的其他jar文件,因此您不應(yīng)將此視為詳盡的列表。 此外,您可以定義自己的屬性。
3. [警告]
   此示例文件僅供參考。 不要將整個(gè)內(nèi)容復(fù)制并粘貼到您的應(yīng)用程序中。 相反,只選擇您需要的屬性。

所以我們?cè)谔砑幽承┡渲脤傩缘臅r(shí)候,一定要根據(jù)自己的需要來(lái)添加,不然有時(shí)出錯(cuò)了,不知道哪里找問(wèn)題。

1. 常用的application.properties配置

一般情況下在src目錄下的/main/resource文件夾中新建application.properties文件,目錄結(jié)構(gòu)如下:

|--src
   |--main
      |--resources
         |--application.properties

我們接下來(lái)編寫(xiě)一些常用的屬性配置,大家在開(kāi)發(fā)中需要到哪些就直接去查看一下:

#啟用調(diào)試日志。
debug=false
#啟用跟蹤日志。
trace=false

#--------------------------------------
# LOGGING 日記
#--------------------------------------
# 日志配置文件的位置。 例如,Logback的classpath:logback.xml
logging.config=classpath:logback.xml
# 日志文件名(例如,`myapp.log`)。名稱可以是精確位置或相對(duì)于當(dāng)前目錄。
logging.file=property.log
# 最大日志文件大小。 僅支持默認(rèn)的logback設(shè)置
logging.file.max-size=10MB
# 日志文件的位置。 例如,`/ var / log`。
logging.path=/var/log

#---------------------------------
# AOP
#---------------------------------
# 使用AOP 切面編程
spring.aop.auto=true
#是否要?jiǎng)?chuàng)建基于子類的(CGLIB)代理(true),而不是基于標(biāo)準(zhǔn)Java接口的代理(false)
spring.aop.proxy-target-class=true

#--------------------------------
# Email
#--------------------------------
# 編碼格式
spring.mail.default-encoding=UTF-8
# SMTP服務(wù)器主機(jī)
spring.mail.host=smtp.property.com
#SMTP服務(wù)器端口
spring.mail.port=7800
# 登錄SMTP用戶名
spring.mail.username=property
# 登錄SMTP密碼
spring.mail.password=123456

#--------------------------------
# WEB 屬性配置
#--------------------------------
# 服務(wù)器應(yīng)綁定的網(wǎng)絡(luò)地址
server.address=127.0.0.1
# 是否啟用了響應(yīng)壓縮
server.compression.enabled=false
# 連接器在關(guān)閉連接之前等待另一個(gè)HTTP請(qǐng)求的時(shí)間。 未設(shè)置時(shí),將使用連接器的特定于容器的默認(rèn)值。 使用值-1表示沒(méi)有(即無(wú)限)超時(shí)
server.connection-timeout=2000
# 錯(cuò)誤控制器的路徑
server.error.path=/error
# 是否啟用HTTP / 2支持,如果當(dāng)前環(huán)境支持它。
server.http2.enabled=false
# 服務(wù)器端口默認(rèn)為:8080
server.port=8084 
# SP servlet的類名。
server.servlet.jsp.class-name=org.apache.jasper.servlet.JspServlet
# 主調(diào)度程序servlet的路徑。
server.servlet.path=/home 
# 會(huì)話cookie名稱
server.servlet.session.cookie.name=propertydemo

#------------------------------
# HTTP encoding
#------------------------------
# HTTP請(qǐng)求和響應(yīng)的字符集。 如果未明確設(shè)置,則添加到“Content-Type”標(biāo)頭。
spring.http.encoding.charset=UTF-8 
# 是否啟用http編碼支持。
spring.http.encoding.enabled=true
#--------------------
# MULTIPART (MultipartProperties)
#--------------------
# 是否啟用分段上傳支持
spring.servlet.multipart.enabled=true
# 上傳文件的中間位置
spring.servlet.multipart.location=/log
# 最大文件的大小
spring.servlet.multipart.max-file-size=1MB
# 最大請(qǐng)求大小
spring.servlet.multipart.max-request-size=10MB
# 是否在文件或參數(shù)訪問(wèn)時(shí)懶惰地解析多部分請(qǐng)求。
spring.servlet.multipart.resolve-lazily=false
#--------------------------------------------
# SPRING SESSION JDBC (JdbcSessionProperties)
#--------------------------------------------
# cron表達(dá)式用于過(guò)期的會(huì)話清理作業(yè)
spring.session.jdbc.cleanup-cron=0 * * * * *
# 數(shù)據(jù)庫(kù)模式初始化模式
spring.session.jdbc.initialize-schema=embedded
# 用于初始化數(shù)據(jù)庫(kù)模式的SQL文件的路徑
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql
# 用于存儲(chǔ)會(huì)話的數(shù)據(jù)庫(kù)表的名稱
spring.session.jdbc.table-name=SPRING_SESSION

#----------------------------------
# MONGODB 數(shù)據(jù)庫(kù)配置
#----------------------------------
# 數(shù)據(jù)庫(kù)名稱
spring.data.mongodb.database=demo
# host 配置
spring.data.mongodb.host=127.0.0.1
# 登錄用戶名
spring.data.mongodb.username=property
# 登錄密碼
spring.data.mongodb.password=123456
# 端口號(hào),自己根據(jù)安裝的mongodb端口配置
spring.data.mongodb.port=9008
# 要啟用的Mongo存儲(chǔ)庫(kù)的類型
spring.data.mongodb.repositories.type=auto
# 連接數(shù)據(jù)uri
spring.data.mongodb.uri=mongodb://localhost/test

#---------------------------------------
# DATASOURCE 數(shù)據(jù)庫(kù)配置
#---------------------------------------
# MySql jdbc Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 連接數(shù)據(jù)庫(kù)
# demo表示的是你創(chuàng)建的數(shù)據(jù)庫(kù);
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&requireSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
# 數(shù)據(jù)庫(kù)用戶名
spring.datasource.username=root
# 數(shù)據(jù)庫(kù)密碼
spring.datasource.password=123456
#-----------------------------------
# Jpa使用
#-----------------------------------
# 目標(biāo)數(shù)據(jù)庫(kù)進(jìn)行操作,默認(rèn)情況下自動(dòng)檢測(cè)。可以使用“databasePlatform”屬性設(shè)置。
#spring.jpa.database= demo1
# 要操作的目標(biāo)數(shù)據(jù)庫(kù)的名稱,默認(rèn)情況下自動(dòng)檢測(cè)。 也可以使用“Database”枚舉來(lái)設(shè)置。
#spring.jpa.database-platform=DEMO
# DDL模式 一般有這幾種方式,Spring Boot會(huì)根據(jù)是否認(rèn)為您的數(shù)據(jù)庫(kù)是嵌入式的,為您選擇一個(gè)默認(rèn)值
# update: 更新架構(gòu)時(shí),使用;
spring.jpa.hibernate.ddl-auto=update
# 是否啟用SQL語(yǔ)句的日志記錄
spring.jpa.show-sql=true

#----------------------------------------
# TESTING PROPERTIES
#----------------------------------------
# 要替換的現(xiàn)有DataSource的類型
spring.test.database.replace=any
# MVC打印選項(xiàng)
spring.test.mockmvc.print=default

# ---------------大家查看文檔進(jìn)行配置,不一一列舉了----------------------

#  各個(gè)屬性注解在查看常用配置文件application.properties中

# FREEMARKER

# DEVTOOLS配置

# SPRING HATEOAS

# HTTP message conversion

# GSON

# JDBC 

# JEST (Elasticsearch HTTP client) (JestProperties)

# CASSANDRA (CassandraProperties)
# --------------------------等等----------------------------------
查看常用配置文件application.properties
2. 自定義屬性

由于有時(shí)為了方便項(xiàng)目的開(kāi)發(fā)維護(hù),我們可能需要到自定義配置屬性,接下來(lái)我們也來(lái)搞一下自定義屬性配置。

application.properties自定義配置屬性:

1.application.properties添加:

#--------------------------------
# 自定義屬性
#--------------------------------
com.eirunye.defproname="root"
com.eirunye.defpropass="123456"

2.在DefPropertyController.class引用

@RestController
public class DefPropertyController {

    @Value("${com.eirunye.defproname}")
    private String defProName;

    @Value("${com.eirunye.defpropass}")
    private String defProPass;

    @RequestMapping(value = "/defproprety")
    public String defPropretyUser() {
        return "這個(gè)自定義屬性名為: " + defProName + ", 密碼為:" + defProPass;
    }
}

注意

在獲取自定義屬性時(shí)一定要嚴(yán)格按照配置文件來(lái)獲取并且Value里面的字符串一定是$+花括號(hào){***},花括號(hào)里面的***表示為:application.properties里面自定義的字符串,所以本例就是表示為: @Value("${com.eirunye.defproname}")
如果application.properties有自定義為 test.ok="haha" ,同樣的獲取方式為:@Value("${test.ok}")

3.測(cè)試這里暫未使用測(cè)試代碼的方式,后面的文章會(huì)講到

IntelliJ IDEA訪問(wèn)

http://localhost:8084/defproprety

Postman訪問(wèn)

通過(guò)Bean的形式獲取

假如我們遇到這樣情況,自定義屬性多,然后每個(gè)都是通過(guò)@Value(${""})方式的話可能會(huì)很容易出錯(cuò),那么我們可以采用以下方式。
1.新建一個(gè)Properties.class
添加@ConfigurationProperties(prefix = "com.eirunye")//表示的是通過(guò)自定義屬性查找,如果自定義是:test.ok=haha,則該這樣表示:@ConfigurationProperties(prefix = "test")

@ConfigurationProperties(prefix = "com.eirunye")//添加該注解
public class Properties {
    private String defproname;
    private String defpropass;
//  get/set方法
    public String getDefproname() {
        return defproname;
    }
    public void setDefproname(String defproname) {
        this.defproname = defproname;
    }
    public String getDefpropass() {
        return defpropass;
    }
    public void setDefpropass(String defpropass) {
        this.defpropass = defpropass;
    }}

2.在controller包下創(chuàng)建 DefBeanPropertyController.class

@RestController
public class DefBeanPropertyController {
   //通過(guò) Autowired注解來(lái)獲取到 Properties屬性,注:Autowired是按類型進(jìn)行裝配,可獲取它所裝配類的屬性
    @Autowired
    Properties properties;

    @RequestMapping(value = "/bean/defproperty")
    public String getDefBeanProperties() {
        return "這是通過(guò)Bean注解的方式獲取屬性: " + properties.getDefproname() + ",密碼為: " + properties.getDefpropass();
    }}

3.在項(xiàng)目的入口文件Application添加注解@EnableConfigurationProperties
最后加上包名不然可能找不到掃描文件如:@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class})

@SpringBootApplication
@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class})//添加注解bean的掃描文件
public class DefpropertysApplication {

    public static void main(String[] args) {
        SpringApplication.run(DefpropertysApplication.class, args);
    }
}

4.測(cè)試
IntelliJ IDEA訪問(wèn)

創(chuàng)建文件xxx.properties文件方式

我們可以自己創(chuàng)建一個(gè)自定義屬性的文件如本例def.properties,(注:一般都是以 .properties 文件結(jié)尾)

1.添加自定義def.properties配置如下:

#--------------------------------
# 自定義屬性
#--------------------------------
# 用戶名
com.eirunye.defineuser="property"
# 年齡
com.eirunye.defineage=20

2.創(chuàng)建 DefineProperties.class

@Configuration
@ConfigurationProperties(prefix = "com.eirunye")//添加注解 ConfigurationProperties "com.eirunye"表示的是自定義屬性
@PropertySource("classpath:defines.properties")// 添加注解 PropertySource 該注解能根據(jù)路徑掃描到我們的文件
public class DefineProperties {
//    這里可以通過(guò)@Value("${}")方式添加,我已經(jīng)屏蔽掉了,直接通過(guò)ConfigurationProperties注解的方式
//    @Value("${com.eirunye.defineuser}")
    private String defineuser;
//    @Value("${com.eirunye.defineage}")
    private int defineage;
// get/set方法
    public String getDefineuser() {
        return defineuser;
    }
    public void setDefineuser(String defineuser) {
        this.defineuser = defineuser;
    }
    public int getDefineage() {
        return defineage;
    }
    public void setDefineage(int defineage) {
        this.defineage = defineage;
    }}

3.在DefinePropertiesController.class引用

@RestController
public class DefinePropertiesController {
    @Autowired
    DefineProperties defineProperties;
    @RequestMapping(value = "define/Properties")
    public String getDefinePropertiesData(){
        return "新建文件自定義屬性姓名:"+defineProperties.getDefineuser()+",新建文件自定義屬性年齡:"+defineProperties.getDefineage();
    }
}

4.別忘了在Application里面添加配置@EnableConfigurationProperties,即:`@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class,com.eirunye.defpropertys.bean.DefineProperties.class})
`
5.測(cè)試

http://localhost:8084/define/Properties

下載application.properties案例demo
二、application.yml配置

由于application.properties配置有點(diǎn)繁瑣,簡(jiǎn)潔是我們非常喜歡的,那么在Spring Boot程序里面當(dāng)然也是可以用.yml文件來(lái)配置的,接下來(lái)讓我們進(jìn)入對(duì).yml文件的一些相關(guān)配置吧,官方文檔Using YAML Instead of Properties。

1.常見(jiàn)的配置

首先在src目錄下的/main/resource文件夾中新建application.yml、application-dev.yml、application-prod.yml三個(gè)文件,刪除application.properties文件,目錄結(jié)構(gòu)如下:

|--src
   |--main
      |--resources
         |--application.yml
         |--application-dev.yml
         |--application-prod.yml

相信很多人要罵街了,這什么情況不是說(shuō),yml配置很給力嗎?怎么還有創(chuàng)建那么多文件,這不是比上面的 application.properties配置還多此一舉嗎?莫急接下來(lái)讓我們來(lái)看看.yml的配置之后,我相信你肯定在項(xiàng)目中會(huì)喜歡用它。

常見(jiàn)的配置屬性如下: 【注意: 這里屬性之間的間隔必須按照要求而來(lái),如:冒號(hào)后面要空格】
application.yml 配置

spring:
  profiles:
    active: dev   #引用 application-dev.yml文件,這里我們可以改為 prod,表示引用application-prod.yml文件
  datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&requireSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
      username: root
      password: 12346
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  data:
    mongodb:
      host: 127.0.0.1
      uri: mongodb://localhost/test
      username: root
      password: 123456
      database: test
  test:
    database:
      replace: any
    mockmvc:
      print: default
  servlet:   
    multipart:
      enabled: true   

application-dev.yml 可以當(dāng)成正式服務(wù)器端口

server:
  port: 8084

application-prod.yml 可以當(dāng)成測(cè)試服務(wù)器端口

server:
  port: 8080

查看更多.yml配置

2.自定義yml配置
在application.yml配置

1.和上面的application.properties類似,但是需要注意的是格式問(wèn)題

com:
  eirunye:
    ymlname: ymlroot
    ymlpass: yml123456

2.通過(guò)@Value("${com.eirunye.ymlname}")獲取

@RestController
public class YmlPropertiesController{
    @Value("${com.eirunye.ymlname}")
    private String ymlname;

    @Value("${com.eirunye.ymlpass}")
    private String ymlpass;

    @RequestMapping(value = "yml/proprety")
    public String getYmlPropreties() {
        return "這個(gè)自定義屬性名為: " + ymlname+ ", 密碼為:" + ymlpass;
    }
}

3.測(cè)試
訪問(wèn):

http://localhost:8084/yml/proprety

通過(guò)Bean方式獲取和application.properties方式一樣

1.創(chuàng)建YmlPropertyBean.class

@ConfigurationProperties(prefix = "com.eirunye")//添加該注解
public class YmlPropertyBean {

    private String ymlname;
    private String ymlpass;
// get/set方法.....
}

2.在 YmlPropertyBeanController.class引用

@RestController
public class YmlPropertyBeanController {
    @Autowired
    YmlPropertyBean propertyBean;
    @RequestMapping(value = "/bean/ymlproperty")
    public String getYmlPropertyData(){
        return "這個(gè)bean自定義屬性名為: " + propertyBean.getYmlname()+ ", 密碼為:" + propertyBean.getYmlpass();
    }
}

3.測(cè)試
訪問(wèn)

http://localhost:8084/bean/ymlproperty

下載.yml案例demo
三、總結(jié)

1.本篇主要講得配置文件,到此就結(jié)束了,在開(kāi)發(fā)中這是我們經(jīng)常用到。
2.在本篇有些相關(guān)配置,本例的代碼實(shí)例還沒(méi)涉及到,接下來(lái)會(huì)繼續(xù)結(jié)合相關(guān)的配置案例繼續(xù)更新。
3.相信大家也有所掌握。

四、Spring Boot系列

大家想學(xué)習(xí)更多關(guān)于Spring Boot系列可以查看

Spring Boot攻略

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/76809.html

相關(guān)文章

  • spring boot - 收藏集 - 掘金

    摘要:引入了新的環(huán)境和概要信息,是一種更揭秘與實(shí)戰(zhàn)六消息隊(duì)列篇掘金本文,講解如何集成,實(shí)現(xiàn)消息隊(duì)列。博客地址揭秘與實(shí)戰(zhàn)二數(shù)據(jù)緩存篇掘金本文,講解如何集成,實(shí)現(xiàn)緩存。 Spring Boot 揭秘與實(shí)戰(zhàn)(九) 應(yīng)用監(jiān)控篇 - HTTP 健康監(jiān)控 - 掘金Health 信息是從 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...

    rollback 評(píng)論0 收藏0
  • 架構(gòu)~微服務(wù) - 收藏集 - 掘金

    摘要:它就是史上最簡(jiǎn)單的教程第三篇服務(wù)消費(fèi)者后端掘金上一篇文章,講述了通過(guò)去消費(fèi)服務(wù),這篇文章主要講述通過(guò)去消費(fèi)服務(wù)。概覽和架構(gòu)設(shè)計(jì)掘金技術(shù)征文后端掘金是基于的一整套實(shí)現(xiàn)微服務(wù)的框架。 Spring Boot 配置文件 – 在坑中實(shí)踐 - 后端 - 掘金作者:泥瓦匠鏈接:Spring Boot 配置文件 – 在坑中實(shí)踐版權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)注明出處本文提綱一、自動(dòng)配置二、自定義屬性三、ran...

    church 評(píng)論0 收藏0
  • 兩年了,我寫(xiě)了這些干貨!

    摘要:開(kāi)公眾號(hào)差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來(lái)越多時(shí),大家搜索起來(lái)就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章系列處理登錄請(qǐng)求前后端分離一使用完美處理權(quán)限問(wèn)題前后端分離二使用完美處理權(quán)限問(wèn)題前后端分離三中密碼加鹽與中異常統(tǒng)一處理 開(kāi)公眾號(hào)差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來(lái)越多時(shí),大家搜索起來(lái)就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<