简体   繁体   中英

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

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 eg (*.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.

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/

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 (eg **/*.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;
    }

This is the solution that I found without overriding PropertySourcesPlaceholderConfigurer. The trick is to implement a PropertySourceFactory with a fake properties file that serves the purpose only to load the factory. You have to put a fake properties file anyway.

@PropertySource(value = "placeholder.properties", factory = MyPropertySourceFactory.class)

At this point you can do anything, but you have no access to Spring context. So you need to use vanilla Java, like using ClassLoader.getResources (but with no wildcard support) or you can use some classpath scanner, like corn-cps.

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