44

I have a spring boot application and I want to read some variable from my application.properties file. In fact below codes do that. But I think there is a good method for this alternative.

Properties prop = new Properties();
InputStream input = null;

try {
    input = new FileInputStream("config.properties");
    prop.load(input);
    gMapReportUrl = prop.getProperty("gMapReportUrl");
} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    ...
}
roottraveller
  • 7,942
  • 7
  • 60
  • 65
Arif Acar
  • 1,461
  • 2
  • 19
  • 33

4 Answers4

55

You can use @PropertySource to externalize your configuration to a properties file. There is number of way to do get properties:

1. Assign the property values to fields by using @Value with PropertySourcesPlaceholderConfigurer to resolve ${} in @Value:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

    @Value("${gMapReportUrl}")
    private String gMapReportUrl;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

2. Get the property values by using Environment:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

    @Autowired
    private Environment env;

    public void foo() {
        env.getProperty("gMapReportUrl");
    }

}

Hope this can help

Wilson
  • 11,339
  • 2
  • 29
  • 33
16

I have created following class

ConfigUtility.java

@Configuration
public class ConfigUtility {

    @Autowired
    private Environment env;

    public String getProperty(String pPropertyKey) {
        return env.getProperty(pPropertyKey);
    }
} 

and called as follow to get application.properties value

myclass.java

@Autowired
private ConfigUtility configUtil;

public AppResponse getDetails() {

  AppResponse response = new AppResponse();
    String email = configUtil.getProperty("emailid");
    return response;        
}

application.properties

emailid=sunny@domain.com

unit tested, working as expected...

Sunny
  • 177
  • 1
  • 2
  • I notice you don't specify a property file name? Does this just load all *.properties files in the classpath? – splashout Feb 23 '23 at 20:36
15

i would suggest the following way:

@PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
@Controller
public class ClassA {
    @Value("${myName}")
    private String name;

    @RequestMapping(value = "/xyz")
    @ResponseBody
    public void getName(){
        System.out.println(name);
    }
}

Here your new properties file name is "otherprops.properties" and the property name is "myName". This is the simplest implementation to access properties file in spring boot version 1.5.8.

sam
  • 1,800
  • 1
  • 25
  • 47
  • I have "sensitive.properties" inside the resources folder, have used the above approach with @Configuration annotation, but it is not able to read value. – Girish Sep 13 '21 at 11:52
  • @Girish please check the location of your properties file. In the above approach, the properties file was in the same location with the application.properties file – sam Sep 14 '21 at 14:37
  • I also have it in the resources folder, but Environment is null and propertysource also not working – Girish Sep 14 '21 at 15:37
  • @Girish this should work actually. please check typographical errors. – sam Sep 14 '21 at 15:41
0

For those of you who want to expose every property from either a .properties or .yml file, Spring Boot automatically creates an endpoint that does just that: endpoint /actuator/env. It is disabled by default though. To enable it, be sure to include env in the application property below: management.endpoints.web.exposure.include=env

You can then filter the properties that you want to keep in the program that reads the endpoint.

Ganael D
  • 77
  • 4