Spring boot - custom variables in Application.properties

76,397

Solution 1

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

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

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

Share:
76,397
Kingamere
Author by

Kingamere

Updated on July 30, 2022

Comments

  • Kingamere
    Kingamere almost 2 years

    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

  • Kingamere
    Kingamere almost 9 years
    I see, but when I enter restAPIServiceAddress as an entry it underlines it in yellow and says "is an unkown property", is that okay?
  • Kingamere
    Kingamere almost 9 years
    Where do I add -Dmy.property ?
  • cahen
    cahen almost 9 years
    which IDE do you use? If it's Eclipse: stackoverflow.com/questions/862391/…
  • cahen
    cahen almost 9 years
    if you're running from the command line, just add it to the end
  • Kingamere
    Kingamere almost 9 years
    ahh ok thanks, and this -Dmy.property and can be added at the end of java -jar my-application.war ?
  • cahen
    cahen almost 9 years
    yes, java -jar <my-app> -Dmy.property=127.127.127.127
  • Stephane Nicoll
    Stephane Nicoll almost 9 years
    I just added an answer that explains what that yellow thing means.
  • Kingamere
    Kingamere almost 9 years
    Wow had no clue about any of this (maybe I should start reading the doc some more), this helped a ton, thank you.
  • Berker Soyluoglu
    Berker Soyluoglu about 8 years
    this i think is the way to go
  • Duncan Jones
    Duncan Jones over 7 years
    I followed these steps (opting for option 1 above), but Spring Tool Suite still marks my property as unknown in the application.properties file. There is a quick fix available to create the application metadata, but I assumed the spring-boot-configuration-processor was supposed to remedy that?
  • Stephane Nicoll
    Stephane Nicoll over 7 years
    You'll have to ask the STS team. All I know is that it should work out-of-the-box. There's not much for us to investigate, create another thread with more details please.
  • Y.M.
    Y.M. over 7 years
    @StephaneNicoll : this is an awesome feature i just discover and i want to thank you and the rest of the team for all the work you have done on spring-boot, keep up the good work ;)
  • guilhermecgs
    guilhermecgs almost 6 years
    I cant argue that this is the right and best way to do it, but just using @zmitrok works just fine as well. Just ignore the yellow warning
  • Hrvoje T
    Hrvoje T over 2 years
    Does this mean I have to add the IP in two places, in FooProperies.java and in application.properties file? Does one overwrite the other? How is this good?
  • Stephane Nicoll
    Stephane Nicoll over 2 years
    No. The one in application properties override the default value in code. If you don't have a default value for the property you don't need this.