Adding Customized Properties in Application.properties file
- In application.properties files, add your own properties such as
mypropertis.version=dev
in the empty space. Here we use
mypropertis
as prefix, you can use whatever you want. - To read such value, create a class as a @Component
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import lombok.Getter; import lombok.Setter; @Getter @Setter @ConfigurationProperties("mypropertis") @Component public class MyProperties { private String version= ""; }
Note that we have use lombok for the getter and setter.
- Since we register our class as a @Component, we can do
@Autowired private MyProperties myProperties;
in our controllers / services
- To get the value, we can do
myProperties.getVersion()
The value woule be “dev” in this case.