0

I am new to Spring-bot and I need to add a configuration file inside the resources folder and read values from it. Right now what I have done is that I have created a 'Configuration.properties' file in the classpath folder and have invoked the properties inside the program

FileReader reader=new FileReader("Configuration.properties");
Properties p=new Properties(); 
p.load(reader);
return p;

Can somebody please help how can i make it to call from the application.properties file of the spring-boot(or similar files) and thus make my configurations available inside the jar which I create.

PS: The Configurations i have given is project specific and am using them for avoiding the hardcoding inside the code.

Albin Chandy
  • 59
  • 1
  • 9
  • 2
    Possible duplicate of [How to access a value defined in the application.properties file in Spring Boot](https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot) – Prog_G Mar 27 '18 at 05:47
  • The answer is correctly explained here[here](https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot) – Prog_G Mar 27 '18 at 05:49

4 Answers4

0

You can use the @Value annotation to get the property file values.

Ex :

@Value("${rest_api_url}")
private String restApiUrl;
0

Create application.properties file in the resource folder, spring boot will automatically find it.

Say you use these properties

test.stringProperty=will_be_used
test.integerProperty=42
stringProperty=not_used

You can then create a configuration properties class like so

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "test")
public static class TestProperties {

    private String stringProperty;
    private Integer integerProperty;

    // getters and setters

}

and use it

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestConfiguration {

    @Autowired
    private TestProperties config;

    // do whatever with properties

}

The autowired object will then have the values you specified in application.propertiesfile with the name 'prefix'.'name of the variable'.

Or you can use the

@Value("${stringProperty}")
private String value;

which is easier at first, but not very maintainable for higher number of properties.

finrod
  • 72
  • 1
  • 9
  • Thanks @finrod , But I am getting null exception once I run the code.May be coz am writing a rest api and the properties call happens in implementor class.Could you please advice how do we extend this method to be implemented in the Spring-Boot App which creates a rest API. – Albin Chandy Mar 27 '18 at 12:52
  • If I understand correctly, you want to use the property value in an implementation of a rest interface. If that's the case, instead of annotating the implementation with `@Component` (or whatever way you use to create the bean) you can create it in the `TestConfiguration` class. Just have a method in the configuration class annotated with `@Bean` that returns a new instance of the implementation and pass the property into it's constructor. – finrod Mar 29 '18 at 08:04
0

Need to define Property file as per requirement of your app. You can use the @Value annotation in bean.
refer : Externalized Configuration and Properties and Configuration

mcacorner
  • 1,304
  • 3
  • 22
  • 45
0

Create a file in resource folder by the name application.properties which will be taken automatically as the default property file.

If you want to specify some other file as a property, do the below-mentioned changes.

Snippet :

@SpringBootApplication
@ImportResource("classpath:filename.properties")
public class Example{
    public static void main(String[] args) {
    SpringApplication.run(Example.class, args);
  }
}
rex roy
  • 1,019
  • 12
  • 5