Adding Customized Properties in Application.properties file

  1. 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.

  2. 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.

  3. Since we register our class as a @Component, we can do
    @Autowired
        private MyProperties myProperties;

    in our controllers / services

  4. To get the value, we can do
    myProperties.getVersion()

    The value woule be “dev” in this case.