原文: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
db.driver=oracle.jdbc.driver.OracleDriver
AppConfig.java
@Configuration
@PropertySource("classpath:db.properties")
public class AppConfig {
 
@Value("${db.driver}")
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
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 
@Configuration
@PropertySource("classpath:db.properties")
public class AppConfig {
 
@Value("${db.driver}")
private String driver;
 
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}

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

<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. 进程间通信七 (socket)

    copyright:weishusheng          data:2015.5.26              摘要:socket又叫套接字或者插口,它也是进程间通信的一种方式,实际上就是网络上 ...

  2. Hibernate4.x之映射关系--一对一映射

    Hibernate的1-1映射关系主要分为两类: 1.Hibernate基于外键映射的1对1关联关系 对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one ...

  3. temorrow read

    http://blog.csdn.net/yimiyangguang1314/article/details/6268177 http://www.cnblogs.com/killmyday/arch ...

  4. 浏览器的CSS Hacks

    LZ注:此文原作者是:Paul Irish(Google的前端开发工程师),本文是原文的部分译文. 我不再使用CSS Hacks了,相反的是,我将使用IE的条件判断将类应用到body标签.   但是, ...

  5. php获取post中的json数据的实现方法

    最近用到腾讯微博与PHP交互,腾讯把json数据post给PHP(腾讯推送数据时,每条数据是一个json格式的数据包,作为post的数据体(请注意post数据体没有参数,不是key=value形式,整 ...

  6. ASP.NET SignalR2持久连接层解析

    越是到年底越是感觉浑身无力,看着啥也不想动,只期盼着年终奖的到来以此来给自己打一针强心剂.估摸着大多数人都跟我一样犯着这样浑身无力的病,感觉今年算是没挣到啥钱,但是话也不能这么说,搞得好像去年挣到钱了 ...

  7. [iOS基础控件 - 3.2] transform的使用

    A.概念 可以控制UIView的位置.尺寸.旋转 transform是一种状态,每次都是相对于原始状态作出的形变     1.位置移动 // 向上移动 - (IBAction)top:(UIButto ...

  8. iOS7滑动返回

    [转载请注明出处] iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势.支持此手势的是UINavigationController中新增的属性 interactivePopGestu ...

  9. Keil MDK 5.0发布了

          从上图可以看出MDK500.EXE (294,308K)分成了两部分,MDK核和software packs.MDK核跟以往一样分成四个部分,编辑器,编译器,包安装,调试跟踪.此版soft ...

  10. Swift 基本语法2

    一.?和! 1.可选类型: ? 在swift中,可选类型(?)其根源是一个枚举型,里面有None和Some两种类型.其实所谓的nil就是Optional.None, 非nil就是Optional.So ...