简体   繁体   中英

How to set propertysource to config file fetched from cloud config server in java spring?

I'm using ConfigurationProperties annotation to map some values from application.yml to java class. Everything works fine when using it with localhost config file, but when I'm fetching configurations from cloud config, those values cannot be found. I guess that problem may be that configuration file name may be different depending on which configuration is chosen and spring don't know in which file look for them.

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

and yaml with configurations

some.prefix:
  variable1: abc
  variable2: xyz

I was trying to map config file via PropertySource annotation but it expects config file name which in my case may be different.

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

Is there any way to pass to PropertySource current loaded config regardless of config file name? Log that I receive after successful configuration fetch from cloud config, for application: web-server profile: LOCAL

Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='file:central-config/web-server-LOCAL.yml'}]}

You can use external configuration into classpath. Use following to pass configuration

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

Or

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

Use Below code to get property values. You can use either of the methods

@Configuration
public class AppConfig {

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

    @Autowired
    Environment env;


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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM