日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

ssm 返回json配置_摆脱困境:将运行时配置作为JSON返回

發(fā)布時(shí)間:2023/12/3 javascript 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ssm 返回json配置_摆脱困境:将运行时配置作为JSON返回 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ssm 返回json配置

如果需要確定部署到遠(yuǎn)程服務(wù)器的Spring Web應(yīng)用程序的運(yùn)行時(shí)配置,則需要讀取從遠(yuǎn)程服務(wù)器找到的屬性文件。 這很麻煩。

幸運(yùn)的是,有更好的方法。 這篇博客文章描述了我們?nèi)绾?

  • 啟動(dòng)我們的Web應(yīng)用程序時(shí),將運(yùn)行時(shí)配置寫入日志文件。
  • 返回運(yùn)行時(shí)配置為JSON。
  • 讓我們開(kāi)始吧。

    如果使用Spring Boot,則應(yīng)使用Spring Boot Actuator 。 它提供了其他功能,可幫助您監(jiān)視和管理Spring Boot應(yīng)用程序。

    如果您還沒(méi)有閱讀我的博客文章,標(biāo)題為:《 從溝壑中反彈:將屬性值注入到配置Bean中》 , 那么您應(yīng)該先閱讀它,然后再繼續(xù)閱讀此博客文章 。 它提供了有助于您理解此博客文章的其他信息。

    將運(yùn)行時(shí)配置寫入日志文件

    通過(guò)執(zhí)行以下步驟,我們可以將運(yùn)行時(shí)配置寫入日志文件:

  • 將toString()方法添加到WebProperties類。
  • 將toString()方法添加到ApplicationProperties類。
  • 啟動(dòng)我們的Web應(yīng)用程序時(shí),將運(yùn)行時(shí)配置寫入日志文件。
  • 讓我們找出如何完成這些步驟。

    首先 ,我們必須在WebProperties類中添加toString()方法,并使用ToStringBuilder類實(shí)現(xiàn)此方法。

    完成此操作后, WebProperties類的源代碼如下所示:

    import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public final class WebProperties {private final String protocol;private final String serverHost;private final int serverPort;@Autowiredpublic WebProperties(@Value("${app.server.protocol}") String protocol,@Value("${app.server.host}") String serverHost,@Value("${app.server.port}") int serverPort) {checkThatProtocolIsValid(protocol);this.protocol = protocol;this.serverHost = serverHost;this.serverPort = serverPort;}private void checkThatProtocolIsValid(String protocol) {if (!protocol.equalsIgnoreCase("http") && !protocol.equalsIgnoreCase("https")) {throw new IllegalArgumentException(String.format("Protocol: %s is not allowed. Allowed protocols are: http and https.",protocol));}}public String getProtocol() {return protocol;}public String getServerHost() {return serverHost;}public int getServerPort() {return serverPort;}@Overridepublic String toString() {return new ToStringBuilder(this).append("protocol", this.protocol).append("serverHost", this.serverHost).append("serverPort", this.serverPort).toString();} }

    其次 ,我們必須將toString()方法添加到ApplicationProperties類并使用ToStringBuilder類實(shí)現(xiàn)它。

    在對(duì)ApplicationProperties類進(jìn)行了這些更改之后,其源代碼如下所示:

    import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component public final class ApplicationProperties {private final String name;private final boolean productionModeEnabled;private final WebProperties webProperties;@Autowiredpublic ApplicationProperties(@Value("${app.name}") String name,@Value("${app.production.mode.enabled:false}") boolean productionModeEnabled,WebProperties webProperties) {this.name = name;this.productionModeEnabled = productionModeEnabled;this.webProperties = webProperties;}public String getName() {return name;}public boolean isProductionModeEnabled() {return productionModeEnabled;}public WebProperties getWebProperties() {return webProperties;}@Overridepublic String toString() {return new ToStringBuilder(this).append("name", this.name).append("productionModeEnabled", this.productionModeEnabled).append("webProperties", this.webProperties).toString();} }

    第三 ,啟動(dòng)應(yīng)用程序時(shí),我們必須將運(yùn)行時(shí)配置寫入日志文件。 我們可以按照以下步驟進(jìn)行操作:

  • 將靜態(tài)的最終Logger字段添加到ApplicationProperties類,并使用LoggerFactory類創(chuàng)建一個(gè)新的Logger對(duì)象。
  • 將writeConfigurationToLog()方法添加到ApplicationProperties類,并使用@PostConstruct注釋對(duì)其進(jìn)行注釋。 這樣可以確保在將創(chuàng)建的bean對(duì)象的依賴項(xiàng)注入到該方法之后調(diào)用該方法。
  • 通過(guò)將配置寫入日志文件來(lái)實(shí)現(xiàn)writeConfigurationToLog()方法。
  • 在對(duì)ApplicationProperties類進(jìn)行了這些更改之后,其源代碼如下所示:

    import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component public final class ApplicationProperties {private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationProperties.class);private final String name;private final boolean productionModeEnabled;private final WebProperties webProperties;@Autowiredpublic ApplicationProperties(@Value("${app.name}") String name,@Value("${app.production.mode.enabled:false}") boolean productionModeEnabled,WebProperties webProperties) {this.name = name;this.productionModeEnabled = productionModeEnabled;this.webProperties = webProperties;}public String getName() {return name;}public boolean isProductionModeEnabled() {return productionModeEnabled;}public WebProperties getWebProperties() {return webProperties;}@Overridepublic String toString() {return new ToStringBuilder(this).append("name", this.name).append("productionModeEnabled", this.productionModeEnabled).append("webProperties", this.webProperties).toString();}@PostConstructpublic void writeConfigurationToLog() {LOGGER.info("Starting application by using configuration: {}", this);} }

    啟動(dòng)Web應(yīng)用程序時(shí),我們應(yīng)該從其日志文件中找到以下信息:

    INFO - ApplicationProperties - Starting application by using configuration: net.petrikainulainen.spring.trenches.config.ApplicationProperties@254449bb[name=Configuration Properties example,productionModeEnabled=false,webProperties=net.petrikainulainen.spring.trenches.config.WebProperties@4e642ee1[protocol=http,serverHost=localhost,serverPort=8080] ]

    該信息寫在一行中,但是我對(duì)它進(jìn)行了格式化,因?yàn)槲蚁胧蛊涓子陂喿x。

    將敏感信息(例如數(shù)據(jù)庫(kù)用戶的用戶名或數(shù)據(jù)庫(kù)用戶的密碼)寫入日志文件不是一個(gè)好主意。

    現(xiàn)在,我們可以從其日志文件中找到Web應(yīng)用程序的運(yùn)行時(shí)配置。 這是對(duì)當(dāng)前情況的改進(jìn),但是只有當(dāng)我們已經(jīng)在讀取日志文件時(shí),它才能使我們的生活更輕松。

    讓我們找出如何通過(guò)實(shí)現(xiàn)將運(yùn)行時(shí)配置返回為JSON的控制器方法來(lái)使生活更加輕松的方法。

    將運(yùn)行時(shí)配置作為JSON返回

    通過(guò)執(zhí)行以下步驟,我們可以實(shí)現(xiàn)一種控制器方法,該方法將運(yùn)行時(shí)配置作為JSON返回:

  • 創(chuàng)建一個(gè)控制器類,并使用@RestController注釋對(duì)其進(jìn)行注釋。
  • 通過(guò)使用構(gòu)造函數(shù)注入,將ApplicationProperties bean注入到創(chuàng)建的控制器bean中。
  • 創(chuàng)建一個(gè)控制器方法來(lái)處理發(fā)送到url'/ config'的GET請(qǐng)求,并通過(guò)返回ApplicationProperties對(duì)象來(lái)實(shí)現(xiàn)它。
  • PropertiesController類的源代碼如下所示:

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;@RestController final class PropertiesController {private final ApplicationProperties applicationProperties;@AutowiredPropertiesController(ApplicationProperties applicationProperties) {this.applicationProperties = applicationProperties;}@RequestMapping(value = "/config", method = RequestMethod.GET)ApplicationProperties getAppConfiguration() {return applicationProperties;} }

    當(dāng)我們將GET請(qǐng)求發(fā)送到url'/ config'時(shí),我們的控制器方法將返回以下JSON:

    {"name":"Configuration Properties example","productionModeEnabled":false,"webProperties":{"protocol":"http","serverHost":"localhost","serverPort":8080} }

    我們不應(yīng)該允許所有人訪問(wèn)我們應(yīng)用程序的配置。 如果這將是一個(gè)真實(shí)的應(yīng)用程序,我們應(yīng)確保只有管理員才能訪問(wèn)此信息。

    讓我們繼續(xù)并總結(jié)從這篇博客文章中學(xué)到的知識(shí)。

    摘要

    這篇博客文章告訴我們:

    • 我們可以通過(guò)重寫配置bean類的toString()方法并將這些bean的屬性值注入到日志文件中后,將運(yùn)行時(shí)配置寫入日志文件。
    • 通過(guò)創(chuàng)建返回“根”配置bean對(duì)象的控制器方法,我們可以將運(yùn)行時(shí)配置作為JSON返回。
    • PS:您可以從Github獲得此博客文章的示例應(yīng)用程序 。

    翻譯自: https://www.javacodegeeks.com/2015/04/spring-from-the-trenches-returning-runtime-configuration-as-json.html

    ssm 返回json配置

    總結(jié)

    以上是生活随笔為你收集整理的ssm 返回json配置_摆脱困境:将运行时配置作为JSON返回的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。