【问题标题】:@ConfigurationProperties not pulling the external properties file@ConfigurationProperties 不提取外部属性文件
【发布时间】:2017-10-08 02:59:55
【问题描述】:

我在 Git 上创建了一个个人存储库,其中保存了我的 application.properties 文件。

我创建了一个云配置服务器 ('my-config-server') 并使用了 git 存储库 url。

我已经绑定了我应该使用 Git 存储库访问外部属性文件的 spring-boot 应用程序。

@javax.jws.WebService(
                  serviceName = "myService",
                  portName = "my_service",
                  targetNamespace = "urn://vdc.com/xmlmessaging/SD",
                  wsdlLocation = "classpath:myService.wsdl",
                  endpointInterface = "com.my.service.SDType")

@PropertySource("application.properties") 
@ConfigurationProperties
public class SDTypeImpl implements SDType {

/*It has various services implementation that use following method**/

private SDObj getObj (BigDecimal value) {
    AnnotationConfigApplicationContext context =
                  new AnnotationConfigApplicationContext(
                          SDTypeImpl.class);
    SDObj obj = context.getBean(SDPropertiesUtil.class).getObj(value);
    context.close();
    return obj;
}

}

另一个类:

public class SDPropertiesUtil {

@Autowired
public Environment env;

public SDObj getObj(BigDecimal value) {

String valueStr = env.getProperty(value.toString());
/*do logic*/ 
}

我的应用程序启动但无法从我的 git 存储库加载属性文件。

我相信我应该在我的应用程序中的 src/main/resources 有一个 application.properties 但因为我正在使用

@PropertySource("application.properties") 
@ConfigurationProperties

我告诉我的应用程序从外部位置使用 application.properties,而不是使用内部属性文件。但这并没有发生。我的应用程序仍在使用内部属性文件。

【问题讨论】:

  • @PraneethRamesh,你知道发生了什么吗?

标签: spring-boot cloud configserver


【解决方案1】:

您包含的源未显示您的应用配置设置以连接到配置服务器。你介意分享一下吗?

这是从客户端应用程序查询配置服务器的方式:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

假设一个配置服务器指向一个包含以下文件的 Git 存储库:demo-config-client-development.properties

您应该能够查询配置服务器:

curl http://localhost:8101/demo-config-client-development.properties

假设 Config Server 在本地运行并监听 8181。

假设您有一个名为:demo-config-client 的客户端应用程序连接到配置服务器并使用 development Spring 配置文件运行,该应用程序现在将能够通过配置服务器读取托管在 Git 存储库中的远程属性。

详细教程可以在我的博客上找到:http://tech.asimio.net/2016/12/09/Centralized-and-Versioned-Configuration-using-Spring-Cloud-Config-Server-and-Git.html

【讨论】:

  • 我使用 cf create-service 命令创建了一个配置服务器.... config-server) 以便在应用推送后将应用与服务器绑定。
  • 然后尝试使用 Spring 配置文件,例如 -Pspring.profiles.active=dev 并将 Git 中的属性文件重命名为 -dev.properties。不需要@PropertySource("application.properties")
  • 我删除了@PropertySource("application.properties"),但是我的spring boot 应用程序将如何理解从Git 引用哪个属性文件以及该文件在Git 中的确切位置?
  • 您能否粘贴应与配置服务器连接的服务配置设置?另外,正如我在上面的答案中提到的,如果您的服务名为 blah,请在 Git 中创建一个名为 blah-dev.properties 的属性文件,然后使用 dev 配置文件运行 blah 服务,例如 java -Dspring.profiles.active= dev -jar 目标/blah.jar
猜你喜欢
  • 2019-03-04
  • 2017-11-14
  • 1970-01-01
  • 2017-06-02
  • 2021-09-22
  • 2016-11-29
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多