Configuring a custom source for Spring feed Download @ConfigurationProperties

I'm new to Spring Downloading and reading about how the @ConfigurationProperties annotation allows you to automatically enter field values โ€‹โ€‹without the @Value annotation.

@Configuration
@ConfigurationProperties(locations = "classpath:some.properties", prefix = "something")
public class MyConfiguration { .. }

I would like to use Groovy ConfigSlurperfor reading my configuration properties. Is there a way to associate @ConfigurationPropertieswith a user property reader, maybe a custom Spring class extension that deals with ConfigSlurper? Or is there a way to simulate the same behavior with another function?

+3
source share
2 answers

, @ConfigurationProperties. @ConfigurationProperties , Environment. locations 1.4 .

, , , , POJO. , locations . , EnvironmentPostProcessor .

+4

, PropertySourceLoader:

public class ConfigSlurperPropertySourceLoader implements PropertySourceLoader {

    @Override
    public String[] getFileExtensions() {
        return new String[] { "groovy" };
    }

    @Override
    public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
        ConfigObject source = new ConfigSlurper(profile).parse(resource.getURL());
        return new ConfigObjectPropertySource(name, source);
    }
}

PropertySource<T> ConfigObject ( ConfigObjectPropertySource). META-INF/spring.factories:

# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.example.ConfigSlurperPropertySourceLoader,\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader

spring-groovy-config github.

+2

All Articles