Reading multiple properties files with @PropertySource (SpringBoot)using wildcard character

可紊 提交于 2021-02-08 09:22:17

问题


I want to read multiples properties files from a specific location, say C:\config. I'm taking help of @PropertySource annotation. Is there a way to read these files in Springboot using some wildcard character e.g (*.properties). So what I intended to achieve is something like this

@PropertySource("*.properties") })
public class SomeClass{
}

If not, Is there a way to create these @PropertySource("foo.properties") or @PropertySource("bar.properties") programmatically and provide them to @PropertySources so that I can achieve this.

@PropertySources({
   @PropertySource("foo.properties"),
   @PropertySource("bar.properties")
})

The reason I want to achieve it so in future if I have to inject another property say future.properties, I do not have to modify the Java files.


回答1:


This might help you:

By command line arguments: By this way you can tell Spring Boot to load our configuration files is by using command arguments. Spring Boot provides the argument spring.config.name to set configuration files names seperated with a comma. The second command line argument is spring.config.location in which you must set the locations where Spring Boot will find your externalised configuration files.

See example below:

java -jar yourApp.jar --spring.config.name=application,conf 
--spring.config.location=classpath:/external/properties/,classpath:/com/yourapp/configuration/



回答2:


PropertySource

Annotation providing a convenient and declarative mechanism for adding a PropertySource to Spring's Environment. To be used in conjunction with @Configuration classes.

Both traditional and XML-based properties file formats are supported — for example, "classpath:/com/myco/app.properties" or "file:/path/to/file.xml".

Resource location wildcards (e.g. **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.

${...} placeholders will be resolved against any/all property sources already registered with the Environment.

@Configuration
@PropertySources({
    @PropertySource("classpath:test.properties"),
    @PropertySource("classpath:test1.properties")
})
public class TestConfig {
    //...
}

You can using to wildcard character. but you need to consider PropertyPlaceholderConfigurer Deprecated.

PropertyPlaceholderConfigurer

Deprecated. as of 5.2; use org.springframework.context.support.PropertySourcesPlaceholderConfigurer instead which is more flexible through taking advantage of the Environment and PropertySource mechanisms.

@Configuration
public class PropertyConfig {

    @Bean
    public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
            throws IOException {
        PropertyPlaceholderConfigurer propertyConfigurer = new PropertyPlaceholderConfigurer();
        propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/test/*.properties"));

        return propertyConfigurer;
    }


来源:https://stackoverflow.com/questions/58601922/reading-multiple-properties-files-with-propertysource-springbootusing-wildcar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!