如何设置propertysource在Java春天从云配置服务器获取配置文件?

问题描述 投票:1回答:1

我使用ConfigurationProperties注释从application.yml一些值映射到Java类。与本地主机配置文件使用时一切正常,但是当我取出由云配置的配置,无法找到这些值。我想这个问题可能是配置文件名可以是在其被选择的配置和春季不知道在哪个文件去找他们不同,这取决于。

@Configuration
@ConfigurationProperties(prefix = "some.prefix")
  public class SomeMappedConfigClass {
    private String variable1;
    private String variable2;
}

并与配置YAML

some.prefix:
  variable1: abc
  variable2: xyz

我试图通过PropertySource注解映射配置文件,但它预计这在我的情况可能会有所不同的配置文件名。

@PropertySource("classpath:some-application.yml")

有什么办法传递给PropertySource当前加载的配置,无论配置文件名?登录我收到后成功配置获取从云配置,应用程序:Web服务器配置文件:LOCAL

Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='file:central-config/web-server-LOCAL.yml'}]}
java spring spring-cloud-config configurationproperties
1个回答
1
投票

您可以使用外部配置到类路径中。使用下面的传递配置

-Dspring.config.location=your/config/dir/

要么

-Dspring.config.location=classpath:prop1.properties,classpath:prop2.properties

使用下面的代码来获得属性值。您可以使用的方法

@Configuration
public class AppConfig {

    @Value("${config.properties:<default values>}")
    String propvalue;

    @Autowired
    Environment env;


    public void method(){
     String datapath = env.getProperty("data.path")
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.