Spring boot - custom variables in Application.properties

SpringSpring MvcSpring BootSpring Data

Spring Problem Overview


I have spring boot client that consumes a restful api. Instead of hardcoding the IP address of the REST API in the java class, is there any key entry in the application.properties I can use?

And if not, can I create a custom entry?

Thanks

Spring Solutions


Solution 1 - Spring

The infrastructure that Spring Boot uses can be used in your own project in the exact same way. You commented in @zmitrok answer about a "unknown property" warning. That is because your property has no meta-data so the IDE does not know about it.

I would strongly advice you not to use @Value if you can as it is rather limited compared to what Spring Boot offers (@Value is a Spring Framework feature).

Start by creating some POJO for your IP:

@ConfigurationProperties("app.foo")
public class FooProperties {

    /**
     * IP of foo service used to blah.
     */
    private String ip = 127.0.0.1;

    // getter & setter
}

Then you have two choices

  1. Put @Component on FooProperties and enable the processing of configuration properties by adding @EnableConfigurationProperties on any of your @Configuration class (this last step is no longer necessary as of Spring Boot 1.3.0.M3)
  2. Leave FooProperties as is and add @EnableConfigurationProperties(FooProperties.class) to any of your @Configuration class which will create a Spring Bean automatically for you.

Once you've done that app.foo.ip can be used in application.properties and you can @Autowired FooProperties in your code to look for the value of the property

@Component
public MyRestClient {

    private final FooProperties fooProperties;

    @Autowired
    public MyRestClient(FooProperties fooProperties) { ... }

    public callFoo() {
       String ip = this.fooProperties.getIp();
       ...
    }

}

Okay so your key is still yellow in your IDE. The last step is to add an extra dependency that will look your code and generate the relevant meta-data at build time. Add the following to your pom.xml

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

And voilà, your key is recognized, you have javadoc and the IDE gives you the default value (the value you initialized on the field). Once you've that you can use any type the conversion service handles (i.e. URL) and the javadoc on the field is used to generate documentation for your keys.

You can also add any JSR-303 constraint validation on your field (for instance a regex to check it's a valid ip).

Check this sample project and the documentation for more details.

Solution 2 - Spring

Instead of hardcoding the IP into the properties file, you can start the application with

-Dmy.property=127.127.10.20

And Spring Boot will automatically pick it up with

@Value("${my.property}")
private String myProperty;

Solution 3 - Spring

You can add your own entries to the application.properties. Just make sure that the property name does not clash with the common properties listed at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKingamereView Question on Stackoverflow
Solution 1 - SpringStephane NicollView Answer on Stackoverflow
Solution 2 - SpringcahenView Answer on Stackoverflow
Solution 3 - SpringzmitrokView Answer on Stackoverflow