Spring Boot - inject map from properties file

24,528

Solution 1

NullPointerException is probably from empty ApplicationProperties.

All custom properties should be annotated @ConfigurationProperties(prefix="custom"). After that, on your main class (class with main method) you must add @EnableConfigurationProperties(CustomProperties.class). For autocomplete you can use:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

If you use @ConfigurationProperties without prefix you use only field name. Field name in you properites. In your case path-mapper, next you specific key and value. Example:

path-mapper.key=value

Remeber after changes in your own properites you need to reload application. Example:

https://github.com/kchrusciel/SpringPropertiesExample

Solution 2

it would be helpful if you can give a more specific example for property file. You should have the same prefix in the url1 and url2 and then you can use

@ConfigurationProperties(prefix="my")

as in

my.pathMapper.url1=path_to_binary1 my.pathMapper.url2=path_to_binary2

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="my")
public class ApplicationProperties {
    private Map<String, String> pathMapper;

    //get and set for pathMapper are important
}

see more at https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading-yaml

Solution 3

There are two things need you need to feed map from properties file. First you need to have a class which has the configuration and target fields to hold data from properties file.

@Configuration
@PropertySource("classpath:myprops.properties")
@ConfigurationProperties("props")
@Component
public class Properties{
   private Map<String,String> map = new HashMap<String,String>();
   // getter setter
}

Secondly define the properties file named myprops.properties with all properties as props

props.map.port = 443
props.map.active = true
props.map.user = aUser
Share:
24,528
gstackoverflow
Author by

gstackoverflow

Updated on April 10, 2020

Comments

  • gstackoverflow
    gstackoverflow about 4 years

    property file looks like this:

    url1=path_to_binary1
    url2=path_to_binary2
    

    According this I tried following approach:

    @Component
    @EnableConfigurationProperties
    public class ApplicationProperties {
        private Map<String, String> pathMapper;
    
        //get and set
    }
    

    and in another component I autowired ApplicationProperties:

    @Autowired
    private ApplicationProperties properties;         
          //inside some method:
          properties.getPathMapper().get(appName);
    

    produces NullPointerException.

    How to correct it?

    update

    I have correct according user7757360 advice:

    @Component
    @EnableConfigurationProperties
    @ConfigurationProperties(prefix="app")
    public class ApplicationProperties {
    

    and properties file:

    app.url1=path_to_binary1
    app.url2=path_to_binary2
    

    Still doesn't work

    Update 2

    @Component
    @EnableConfigurationProperties
    @ConfigurationProperties(prefix="app")
    public class ApplicationProperties {
        private Map<String, String> app;
    

    and inside application.properties:

    app.url1=path_to_binary1
    app.url2=path_to_binary2
    

    Still doesn't work