当前位置: 首页 > news >正文

宿迁怎样建设网站青岛网站建设公司好找吗

宿迁怎样建设网站,青岛网站建设公司好找吗,WordPress写文章一直转,建做网站如果需要确定部署到远程服务器的Spring Web应用程序的运行时配置#xff0c;则需要读取从远程服务器找到的属性文件。 这很麻烦。 幸运的是#xff0c;有更好的方法。 这篇博客文章描述了我们如何 启动我们的Web应用程序时#xff0c;将运行时配置写入日志文件。 返回运行… 如果需要确定部署到远程服务器的Spring Web应用程序的运行时配置则需要读取从远程服务器找到的属性文件。 这很麻烦。 幸运的是有更好的方法。 这篇博客文章描述了我们如何 启动我们的Web应用程序时将运行时配置写入日志文件。 返回运行时配置为JSON。 让我们开始吧。 如果使用Spring Boot则应使用Spring Boot Actuator 。 它提供了其他功能可帮助您监视和管理Spring Boot应用程序。 如果您还没有阅读我的博客文章标题为 从槽中反弹将属性值注入配置Bean中 那么您应该先阅读它然后再继续阅读此博客文章 。 它提供了有助于您理解此博客文章的其他信息。 将运行时配置写入日志文件 我们可以按照以下步骤将运行时配置写入日志文件 将toString方法添加到WebProperties类。 将toString方法添加到ApplicationProperties类。 启动我们的Web应用程序时将运行时配置写入日志文件。 让我们找出如何完成这些步骤。 首先 我们必须向WebProperties类中添加toString方法并使用ToStringBuilder类实现此方法。 完成此操作后 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类实现它。 在对ApplicationProperties类进行了这些更改之后其源代码如下所示 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();} } 第三 我们必须在启动应用程序时将运行时配置写入日志文件。 我们可以按照以下步骤进行操作 将静态的最终Logger字段添加到ApplicationProperties类并使用LoggerFactory类创建一个新的Logger对象。 将writeConfigurationToLog方法添加到ApplicationProperties类并使用PostConstruct注释对其进行注释。 这样可以确保在将创建的bean对象的依赖项注入到该方法之后调用该方法。 通过将配置写入日志文件来实现writeConfigurationToLog方法。 在对ApplicationProperties类进行了这些更改之后其源代码如下所示 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);} } 启动Web应用程序时应从其日志文件中找到以下信息 INFO - ApplicationProperties - Starting application by using configuration: net.petrikainulainen.spring.trenches.config.ApplicationProperties254449bb[nameConfiguration Properties example,productionModeEnabledfalse,webPropertiesnet.petrikainulainen.spring.trenches.config.WebProperties4e642ee1[protocolhttp,serverHostlocalhost,serverPort8080] ] 该信息写在一行中但是我对其进行了格式化因为我想使其更易于阅读。 将敏感信息例如数据库用户的用户名或数据库用户的密码写入日志文件不是一个好主意。 现在我们可以从其日志文件中找到Web应用程序的运行时配置。 这是对当前情况的改进但是只有当我们已经在读取日志文件时它才能使我们的生活更轻松。 让我们发现如何通过实现将运行时配置返回为JSON的控制器方法来使我们的生活更加轻松。 将运行时配置作为JSON返回 通过执行以下步骤我们可以实现一种控制器方法该方法将运行时配置作为JSON返回 创建一个控制器类并使用RestController注释对其进行注释。 通过使用构造函数注入将ApplicationProperties bean注入到创建的控制器bean中。 创建一个控制器方法来处理发送到url/ config的GET请求并通过返回ApplicationProperties对象来实现它。 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;} } 当我们将GET请求发送到url/ config时我们的控制器方法将返回以下JSON {name:Configuration Properties example,productionModeEnabled:false,webProperties:{protocol:http,serverHost:localhost,serverPort:8080} } 我们不应该允许所有人访问我们应用程序的配置。 如果这将是一个真实的应用程序则应确保只有管理员才能访问此信息。 让我们继续并总结从这篇博客文章中学到的知识。 摘要 这篇博客文章告诉我们 我们可以通过重写配置bean类的toString方法并将这些bean的属性值注入到日志文件中后将运行时配置写入日志文件。 通过创建返回“根”配置bean对象的控制器方法我们可以将运行时配置作为JSON返回。 PS您可以从Github获得此博客文章的示例应用程序 。 翻译自: https://www.javacodegeeks.com/2015/04/spring-from-the-trenches-returning-runtime-configuration-as-json.html
http://www.zqtcl.cn/news/846033/

相关文章:

  • 免费行情软件app网站红色西安做网站印象网络
  • 宁波网站建设小程序开发聊城wap网站建设
  • 陇南网站网站建设泰安网站的建设
  • 哪个网站有介绍拿到家做的手工活建设银行网站怎么修改手机号码吗
  • 网站地图怎么用淘宝客推广网站建设
  • 外贸零售网站建设购物网站支付功能怎么做
  • 淘宝客如何做自己的网站西宁工程建设招聘信息网站
  • 天津都有哪些制作网站郑州官网首页
  • 个人网站开发模式海南省建设公司官网
  • edu网站开发做爰视频在线观看免费网站
  • 安防公司网站模板网站建设模板下载
  • 贵阳网站建设方案维护一 建设茶叶网站前的市场分析
  • 山东东营建设网官方网站百度电脑版
  • 做网站前途如何海尔网站建设推广
  • 投资公司网站建设万网域名安装wordpress
  • 高端网站建设企业官网建设wordpress相似推荐
  • php网站开发师招聘wordpress怎么换头像
  • 门禁考勤网站建设广西建设
  • 互助盘网站怎么做的织梦免费企业网站
  • 做羊毛毡的网站电子商务网站建设品牌
  • 用vue做商城网站常用的js教做发型的网站
  • 江西省寻乌县建设局网站广州网站建设一般多少钱
  • 做网站公司郑州郑州的网站建设公司哪家好网站开发word
  • 网页转向功能网站wordpress搭建小说站
  • 北京华夏建设有限公司网站wordpress建站安全吗
  • 怎样做电子商务网站直接通过ip访问网站
  • 白沟17网站一起做网店有啥方法下载wordpress主题
  • 找人做网站毕业设计用于做网站头的图片
  • 黄埔做网站江西省建设工程造价管理局网站
  • 适合网站开发的框架网盘视频直接做网站