36

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,264
  • 7
  • 55
  • 65
Arif Acar
  • 1,272
  • 2
  • 17
  • 29

4 Answers4

48

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
  • 10,739
  • 2
  • 23
  • 32
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
  • 169
  • 1
  • 2
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,609
  • 1
  • 22
  • 44
  • 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
4

We can read properties file in spring boot using 3 way

1. Read value from application.properties Using @Value

map key as

 public class EmailService {
    
     @Value("${email.username}")
     private String username;
}

2. Read value from application.properties Using @ConfigurationProperties

In this we will map prefix of key using ConfigurationProperties and key name is same as field of class

  @Component
   @ConfigurationProperties("email")
    public class EmailConfig {
 
        private String   username;
    }

3. Read application.properties Using using Environment object

public class EmailController {
    
    @Autowired
    private Environment env;

    @GetMapping("/sendmail")
    public void sendMail(){     
        System.out.println("reading value from application properties file  using Environment ");
        System.out.println("username ="+ env.getProperty("email.username"));
        System.out.println("pwd ="+ env.getProperty("email.pwd"));
    }

Reference : how to read value from application.properties in spring boot

Hasan Khan
  • 486
  • 1
  • 6
  • 19
Anuj Dhiman
  • 2,060
  • 1
  • 20
  • 44