详解Spring通过@Value注解注入属性的几种方式
场景
假如有以下属性文件dev.properties,需要注入下面的tag
tag=123
通过PropertyPlaceholderConfigurer
代码
@Value("${tag}")
privateStringtag;
通过PreferencesPlaceholderConfigurer
代码:
@Value("${tag}")
privateStringtag;
通过PropertiesFactoryBean
代码:
@Value("#{config['tag']}")
privateStringtag;
通过util:properties
效果同PropertiesFactoryBean一样
代码:
@Value("#{config['tag']}")
privateStringtag;
其他方式
有时也可以不通过文件,直接写字面量
123
代码:
@Value("${tag}")
privateStringtag;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短