5

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

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 2
    You can find a better answer here: [How to fill HashMap from java property file with Spring @Value](https://stackoverflow.com/questions/28369458/how-to-fill-hashmap-from-java-property-file-with-spring-value) – Pino Oct 28 '20 at 12:58

4 Answers4

7

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

lrv
  • 261
  • 2
  • 5
  • Where to write properties file name? – gstackoverflow Mar 29 '17 at 16:04
  • based your update your properties file should be ```app.app.url1=path_to_binary1 app.app.url2=path_to_binary2``` I recommend using a different prefix – lrv Mar 29 '17 at 16:31
  • assuming you use defaults, spring-boot looks for the default name ``application.properties`` so there is no need to write the properties file name anywhere. Just ensure ``application.properties`` is in ``src/main/resources`` as shown in the example provided by @kchrusciel – lrv Mar 29 '17 at 16:48
3

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

kchrusciel
  • 143
  • 7
1

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
Chandan Kumar
  • 1,066
  • 10
  • 16
0

Have the your.properties file under src/main/resources and either have it as a

@Configuration
@PropertySource("classpath:your.properties")
public class SpringConfig(){}

or have it as a PropertyPlaceholderConfigurer in your Spring yourApplicationContext.xml.

Then use the property values like

@Value("app.url1")
String path_to_binary1;

@Value("app.url2")
String path_to_binary2;

// ...

System.out.println(path_to_binary1+path_to_binary2);