0

I find @ConfigurationProperties annotation very useful to get a HashMap out of my properties when I work with Spring Boot. But I have this old project which is using Spring 4.2.9RELEASE version which I cannot change and apparently it donot have @ConfigurationProperties or I am not aware of its dependency in 4.2.9RELEASE. Could anyone suggest the best way to have it in the project. If I include spring boot dependency along with spring 4.2.9RELEASE it is not creating any conflicts as of now but is it advisable to do that? Below is my POM:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <spring.version>4.2.9.RELEASE</spring.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
     </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
     </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
     </dependency>
</dependencies>
Harsh
  • 445
  • 1
  • 5
  • 13
  • `@ConfigurationProperties` comes from Boot and [is found in the `spring-boot` dependency](http://search.maven.org/#search%7Cga%7C1%7Cfc%3A%22org.springframework.boot.context.properties.ConfigurationProperties%22). What exactly makes you think that you "do not have" it? – chrylis -cautiouslyoptimistic- Dec 15 '17 at 15:10
  • I odon't think it is a good idea to include spring-boot dependency just for the sake of configurationproperties. Because it loads too many things which are not needed. You can achieve what you want even without ConfigurationProperties(may be with a bit of code) – pvpkiran Dec 15 '17 at 15:17
  • @pvpkiran Thanks for your feedback. I ended up with writing a property mapper to achieve my requirement. This post was very helpful https://stackoverflow.com/a/28413642/1343735 – Harsh Dec 19 '17 at 09:37

1 Answers1

0

There is a getPropertyNames() method in the abstract class EnumerablePropertySource which can be useful.

Here I get all the properties starting with "prefix" from the application.properties file.

@PropertySource("classpath:application.properties")
public class AppConfiguration {

    @Autowired
    ConfigurableEnvironment env;

    @Bean("props")
    public Properties props() {
        Properties properties = new Properties();
        org.springframework.core.env.PropertySource<?> propertySource =
                env.getPropertySources().get("class path resource [application.properties]");
        for (String k : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
            if (k.startsWith("prefix")) {
                properties.setProperty(k, (String) propertySource.getProperty(k));
            }
        };
        return properties;
    }
}

Note that Spring Boot's @ConfigurationProperties removes the prefix, but the code above preserves it.

piakbt
  • 1
  • 1