原文:http://www.mkyong.com/spring/spring-is-not-working-in-value/

By mkyong | February 4, 2015 | Last Updated : February 12, 2015

A simple Spring @PropertySource example to read a properties file.

db.properties
  1. db.driver=oracle.jdbc.driver.OracleDriver
AppConfig.java
  1. @Configuration
  2. @PropertySource("classpath:db.properties")
  3. public class AppConfig {
  4.  
  5. @Value("${db.driver}")
  6. private String driver;

But the property placeholder ${} is unable to resolve in @Value, if print out the driver variable, it will display string ${db.driver} directly, instead of “oracle.jdbc.driver.OracleDriver”.

Solution

To resolve ${} in Spring @Value, you need to declare a STATICPropertySourcesPlaceholderConfigurer bean manually. For example :

AppConfig.java
  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  6.  
  7. @Configuration
  8. @PropertySource("classpath:db.properties")
  9. public class AppConfig {
  10.  
  11. @Value("${db.driver}")
  12. private String driver;
  13.  
  14. @Bean
  15. public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
  16. return new PropertySourcesPlaceholderConfigurer();
  17. }
  18. }

For XML configuration, Spring will help you to register PropertySourcesPlaceholderConfigurerautomatically.

  1. <util:properties location="classpath:db.properties"/>

Spring – ${} is not working in @Value--转载的更多相关文章

  1. [转载]架构指南 : Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4

    1. 环境配置 a)         Java 1.7 b)         Eclipse luna c)         Maven3.2.5 d)         spring 4.1.4 2. ...

  2. (转载)自己实现spring

    您还 « 上一页 1 2 3 下一页 » 浏览 9671 次 锁定老帖子 主题:spring深入源码1 简单实现ioc机制 精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 ( ...

  3. 转载:spring ,struct2 在 web.xml中的配置

    转载网址:http://blog.sina.com.cn/s/blog_4c6e822d0102dv63.html <!-- Struts2 need begin-->  <filt ...

  4. Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

    作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...

  5. RTP与RTCP协议介绍(转载)

    RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步.RTP中没有连接的概念,本身并不能为按序传输数据包提供可靠的保证,也不提供流量控制和拥塞控制,这些都由RTCP来负责完 ...

  6. 《Walking the callstack(转载)》

    本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with so ...

  7. [转载]MVVM模式原理分析及实践

    没有找到很好的MVVM模式介绍文章,简单找了一篇,分享一下.MVVM实现了UI\UE设计师(Expression Blend 4设计界面)和软件工程师的合理分工,在SilverLight.WPF.Wi ...

  8. [转载]:STM32为什么必须先配置时钟再配置GPIO

    转载来源 :http://blog.csdn.net/fushiqianxun/article/details/7926442 [原创]:我来添两句,就是很多同学(包括我)之前搞低端单片机,到了stm ...

  9. [转载]从MyEclipse到IntelliJ IDEA-让你摆脱鼠标,全键盘操作

    从MyEclipse转战到IntelliJ IDEA的经历 注转载址:http://blog.csdn.net/luoweifu/article/details/13985835 我一个朋友写了一篇“ ...

  10. TCP同步与异步,长连接与短连接【转载】

    原文地址:TCP同步与异步,长连接与短连接作者:1984346023 [转载说明:http://zjj1211.blog.51cto.com/1812544/373896   这是今天看到的一篇讲到T ...

随机推荐

  1. GitHub进一步了解

    为什么要引入Git: 1)用简单一点的例子来讲: 一个公司有几个甚至十几个人共同合作开发一款项目的时候,公司对项目代码有明确规范,不可能再像学校做的小项目一样,你觉得哪个同学的代码写的不好,或者和你的 ...

  2. JPA--联合主键

    联合主键的一些知识: 使用@EmbeddedId标示联合主键: 在联合主键类中只是定义确定联合主键的字段即可: * 联合主键类的规则 * 1.必须包含一个无参的构造函数 * 2.必须实现序列化接口 * ...

  3. C#实现无物理边距真正可打印区域的绘图\打印程序开发

    经常在开发实际的应用程序中,需要用到图形绘制和打印程序.如何实现完整的精确打印和绘图是需要注意许多细节地方的.最近在遇到打印问题的时候,仔细研究一阵,总结这篇博文,写得有点杂乱,看文要还请费点神. 基 ...

  4. HTML DOM 教程Part1

    2015-05-08 摘自W3C School HTML DOM HTML DOM 定义了访问和操作HTML文档的标准方法.HTML DOM 把 HTML 文档呈现为带有元素.属性和文本的树结构(节点 ...

  5. HDU 5696 区间的价值 暴力

    分析:枚举以该点为最小值,然后找到最大值,更新,相应数组,至此,把区间分割(因为包含这个最小值点已经枚举过) 然后理解理解,感觉好像挺对的...因为后面(即dfs区间变小时)如果再包含最小值节点,也不 ...

  6. FOJ 1608 Huge Mission 线段树

    每个节点维护一个最小值,更新发现如果大于最小值,直接向下更新.速度还可以.. #include<cstdio> #include<algorithm> #include< ...

  7. 解读四大移动web应用开发框架真相

    [51CTO译文]近来关于新的移动网页框架及移动平台存在不少争论.平心而论,这些工具在条款内容方面的混乱与模糊也是造成大家误解的原因之一.我希望通过几条简短的评述来尽量清理这种认识层面上的混乱状态. ...

  8. 关于在MDK中使用 printf 函数

    microlib 提供了一个有限的 stdio 子系统,它仅支持未缓冲的 stdin.stdout 和 stderr. 这样,即可使用 printf() 来显示应用程序中的诊断消息. 要使用高级 I/ ...

  9. cocos2d-x编译到android平台后,增加返回键和菜单键支持

    在头文件中增加函数 virtual void keyBackClicked();//android返回键 virtual void keyMenuClicked();//android菜单键 在ini ...

  10. 6.2 CUDA streams

    stream是什么 nivdia给出的解释是:A sequence of operations that execute in issue-order on the GPU.  可以理解成在GPU上执 ...