Spring @value用法示例详解
作者:毛毛的猫毛 时间:2022-09-29 03:03:48
为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性。
一、两种使用方法
1、@Value(“#{configProperties[‘key’]}”)
2、@Value(“${key}”)
二、配置
2.1 @Value(“#{configProperties[‘key’]}”)使用
2.1.1配置文件:
配置方法1:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:value.properties</value>
</list>
</property>
</bean>
配置方法2:
<util:properties id="configProperties" location="classpath:value.properties"></util:properties>
注:配置1和配置2等价,这种方法需要util标签,要引入util的xsd:
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
value.properties
key=1
ValueDemo.java
@Component
public class ValueDemo {
@Value("#{configProperties['key']}")
private String value;
public String getValue() {
return value;
}
}
2.2 @Value(“${key}”)使用
2.2.1 配置文件
1、在2.1.1的配置文件基础上增加:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
2、直接指定配置文件,完整的配置:
<bean id="appProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:value.properties</value>
</array>
</property>
</bean>
ValueDemo.java
@Component
public class ValueDemo {
@Value("${key}")
private String value;
public String getValue() {
return value;
}
}
来源:https://blog.csdn.net/m0_67400973/article/details/126505535
标签:Spring,@value
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
winform绑定快捷键的方法
2023-12-10 22:16:04
Java毕业设计实战项目之在线服装销售商城系统的实现流程
2023-03-27 00:15:31
![](https://img.aspxhome.com/file/2023/6/71816_0s.jpg)
Java进程cpu占用过高问题解决
2021-08-09 00:16:59
![](https://img.aspxhome.com/file/2023/7/70337_0s.jpg)
浅析Java内存模型与垃圾回收
2023-11-23 06:11:58
![](https://img.aspxhome.com/file/2023/5/59965_0s.png)
java开源项目jeecgboot的超详细解析
2023-07-19 03:30:53
![](https://img.aspxhome.com/file/2023/7/71677_0s.png)
JavaWeb使用Session和Cookie实现登录认证
2023-12-11 19:13:29
Flutter模仿实现微信底部导航栏流程详解
2023-06-21 11:46:12
![](https://img.aspxhome.com/file/2023/3/100603_0s.png)
eclipse maven 插件的安装和配置详解
2023-08-24 16:57:01
![](https://img.aspxhome.com/file/2023/8/58298_0s.jpg)
搭建MyBatis-Plus框架并进行数据库增删改查功能
2023-11-09 04:33:43
![](https://img.aspxhome.com/file/2023/7/59167_0s.png)
Java动态代理详解及实例
2023-12-08 15:51:03
RocketMQ源码分析之Broker过期消息清理机制
2023-06-10 15:47:43
解决MyEclipse中的Building workspace问题的三个方法
2023-10-25 09:00:51
![](https://img.aspxhome.com/file/2023/2/81252_0s.png)
深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格
2023-05-25 04:52:01
![](https://img.aspxhome.com/file/2023/8/84738_0s.gif)
Java使用Freemarker页面静态化生成的实现
2022-07-24 08:48:42
![](https://img.aspxhome.com/file/2023/9/63069_0s.png)
java求余的技巧汇总
2023-08-06 06:33:16
![](https://img.aspxhome.com/file/2023/8/57898_0s.jpg)
vs2019永久配置opencv开发环境的方法步骤
2023-11-02 19:38:58
![](https://img.aspxhome.com/file/2023/2/100302_0s.jpg)
Java数组越界问题实例解析
2023-10-25 18:16:23
深入Java Final
2022-06-22 09:38:14
轻松掌握Java建造者模式
2023-11-06 15:24:38
JavaWeb开发之使用jQuery与Ajax实现动态联级菜单效果
2023-11-28 19:46:08
![](https://img.aspxhome.com/file/2023/6/60466_0s.png)