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

小说网站怎么做局网站建设管理整改情况

小说网站怎么做,局网站建设管理整改情况,自己如何制作网页,直播小程序源码ssm 返回json配置如果需要确定部署到远程服务器的Spring Web应用程序的运行时配置#xff0c;则需要读取从远程服务器找到的属性文件。 这很麻烦。 幸运的是#xff0c;有更好的方法。 这篇博客文章描述了我们如何 启动我们的Web应用程序时#xff0c;将运行时配置写入日志… ssm 返回json配置 如果需要确定部署到远程服务器的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.htmlssm 返回json配置
http://www.huolong8.cn/news/208697/

相关文章:

  • 网站排版中铁三局招聘2022
  • Wordpress网站能做seo吗宁波网站建设公司名单推荐
  • 常州手机网站开发网站的ftp帐号密码
  • 网站建设公司黄页wordpress seo 百度
  • 网站设计建设网站做网站 哪里发布
  • 有网站源码如何搭建自己的网站网络营销app有哪些
  • 惠州技术支持网站建设手机网站制作招聘
  • 免费自建网站工具网站建设好后能修改吗
  • 没有备案的网站怎么挂广告建站系统多少钱
  • 长沙武广新城建设网站广东广州免费建站
  • 做seo网站优化价格wordpress会员推广插件
  • 北京怀柔网站建设公司动画设计思路怎么写
  • 魅族的网站建设与安全医疗网站建设哪个好用
  • 长沙专业网站制作如何制作教学视频
  • 鱼台县建设局网站房产网签合同
  • 网站ip地址查询域名资源网站哪个好
  • 常用的网站建设技术有什么婚纱摄影网站开发
  • 作风建设 宣讲家网站太原网站制作案例
  • 网站设计 公司 长沙私人设计工作室前景
  • 建设网站虚拟主机免费下载app软件正版
  • 农业建设公司网站网站设计标准
  • 上海网站建设哪家技术好湖南微信网站公司简介
  • 零陵做网站十大网络推广公司
  • 湖北网站建设贴吧桐梓县工程建设交易网站
  • 深圳网站建 1设骏域网站建设ps做 网站标准尺寸
  • 网站建设小程序开发报价湘潭网站建设 w磐石网络
  • php开发一个企业网站价格wordpress 破解商场主题
  • 做外掛网站空间网站建设培训班上的讲话
  • 做网站要钱吗网站做外链多少钱
  • 遂溪 网站建设监理网站