1

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'}]}
Paweł Sosnowski
  • 173
  • 3
  • 12
  • 1
    there is no issue here. add spring boot actuator to view the properties binded to the application. Or check the config server on what properties retrieved ```http://localhost:8888/{application}/{profile}``` – Barath Feb 07 '19 at 17:41
  • I verified it and you were right, spring loaded config correctly and I had some issues with configurations, thanks – Paweł Sosnowski Feb 08 '19 at 12:45

1 Answers1

1

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")
    }

}
Debopam
  • 3,198
  • 6
  • 41
  • 72
  • 1
    actually I didn't want to use @Value but I read more about spring environment and how it works, and I figured out that spring load configs correctly and I had some issues with loaded config, thanks – Paweł Sosnowski Feb 08 '19 at 12:43