宿迁怎样建设网站,青岛网站建设公司好找吗,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