Spring通过@Value注解注入属性的几种方式
来源:互联网 发布:抗美援朝有必要吗 知乎 编辑:程序博客网 时间:2024/04/30 11:08
来源:http://blog.csdn.net/csujiangyu/article/details/50945486
目录(?)[+]
场景
假如有以下属性文件dev.properties, 需要注入下面的tag
tag=123
通过PropertyPlaceholderConfigurer
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="dev.properties" /></bean>
代码
@Value("${tag}")private String tag;
通过PreferencesPlaceholderConfigurer
<bean id="appConfig" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="location" value="dev.properties" /></bean>
代码:
@Value("${tag}")private String tag;
通过PropertiesFactoryBean
- 1
- 2
- 3
- 1
- 2
- 3
代码:
@Value("#{config['tag']}")private String tag;
通过util:properties
效果同PropertiesFactoryBean一样
代码:
@Value("#{config['tag']}")private String tag;
其他方式
有时也可以不通过文件,直接写字面量
<bean id="appConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!--<property name="location" value="classpath:${env}.properties" />--> <property name="properties"> <props> <prop key="tag">123</prop> </props> </property></bean>
代码:
@Value("${tag}")private String tag;
目录(?)[+]
场景
假如有以下属性文件dev.properties, 需要注入下面的tag
tag=123
通过PropertyPlaceholderConfigurer
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="dev.properties" /></bean>
代码
@Value("${tag}")private String tag;
通过PreferencesPlaceholderConfigurer
<bean id="appConfig" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="location" value="dev.properties" /></bean>
代码:
@Value("${tag}")private String tag;
通过PropertiesFactoryBean
- 1
- 2
- 3
- 1
- 2
- 3
代码:
@Value("#{config['tag']}")private String tag;
通过util:properties
效果同PropertiesFactoryBean一样
代码:
@Value("#{config['tag']}")private String tag;
其他方式
有时也可以不通过文件,直接写字面量
<bean id="appConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!--<property name="location" value="classpath:${env}.properties" />--> <property name="properties"> <props> <prop key="tag">123</prop> </props> </property></bean>
代码:
@Value("${tag}")private String tag;
1 0
- Spring通过@Value注解注入属性的几种方式
- Spring通过@Value注解注入属性的几种方式
- Spring通过@Value注解注入属性的几种方式
- Spring通过@Value注解注入属性的几种方式
- Spring如何通过注解方式注入静态属性
- Spring中属性注入的几种方式以及复杂属性的注入
- 通过继承来实现注解方式的属性注入
- spring通过三种方式将value注入到bean的property中
- spring详解:使用注解方式注入属性
- spring的注解注入方式
- Spring依赖注入的几种方式
- Spring几种注入方式的记录
- spring map 注入的几种方式
- Spring注入Bean的几种方式
- Spring的几种注入方式
- Spring的几种注入方式
- spring的依赖注入几种方式
- spring的几种注入方式
- memcpy与memmove
- 地址转译的相关问题(二)
- Android studio中按钮字母大小写问题
- oracle安装--db_install.rsp详解
- 每个程序员都应该了解的内存知识3——虚拟内存
- Spring通过@Value注解注入属性的几种方式
- Xcode结合iTunes快速打包制作ipa
- ArcSDE10.2在SQL Server上的配置与连接
- 【工作笔记】C语言的字符串真麻烦,好好梳理下
- iOS App让自己的应用在其他应用中打开列表中显示、iOS把自己的应用添加到”活动“、将PDF文件Open In MyApp
- select基本的取值,赋值及清空
- 【ES6】Proxy
- oracle安装--netca.rsp详解
- android 电容屏(一):电容屏基本原理篇