1.使用 @Value("${...}") 方式:

可以获取属性文件中对应的值(如果属性文件中没有这个属性,则会报错。可以通过赋予默认值解决这个问题,如@Value("${attr:127.0.0.1}")


2.使用 @Value("#{...}")方式:

#{…}的{}里面的内容必须符合SpEL表达式,可以是文字表达式、Bean属性和方法、类表达式、访问properties(如:systemProperties 和 systemEnvironment),具体可以参考:点击打开链接


3.在@Value(...)中 ${…}和#{…}混合使用

注意必须#{}外面,${}在里面,因为spring执行${}是时机要早于#{}



具体实例如下:


项目结构:

springboot下@Value属性注入简单使用


application.properties属性文件:

springboot下@Value属性注入简单使用


Test.java文件:

springboot下@Value属性注入简单使用


SpringbootSpelApplication文件:

springboot下@Value属性注入简单使用


相关文章:

  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2022-02-09
  • 2022-01-23
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
猜你喜欢
  • 2019-12-25
  • 2022-12-23
  • 2022-02-01
  • 2021-05-22
  • 2021-07-21
相关资源
相似解决方案