Spring Boot @Value Properties

后端 未结 8 1152
刺人心
刺人心 2020-12-10 10:05

I have a Spring Boot application and in one of the classes, I try to reference a property from the application.properties file using @Value. But, the property d

相关标签:
8条回答
  • 2020-12-10 10:55

    I had the similar issue and the above examples doesn't help me to read properties. I have posted the complete class which will help you to read properties values from application.properties file in SpringBoot application in the below link.

    Spring Boot - Environment @Autowired throws NullPointerException

    0 讨论(0)
  • 2020-12-10 11:00

    Make sure your application.properties file is under src/main/resources/application.properties. Is one way to go. Then add @PostConstruct as follows

    Sample Application.properties

    file.directory = somePlaceOverHere
    

    Sample Java Class

    @ComponentScan
    public class PrintProperty {
    
      @Value("${file.directory}")
      private String fileDirectory;
    
      @PostConstruct
      public void print() {
        System.out.println(fileDirectory);
      }
    }
    

    Code above will print out "somePlaceOverhere"

    0 讨论(0)
提交回复
热议问题