zoukankan      html  css  js  c++  java
  • Spring – ${} is not working in @Value--转载

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

    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"/>
  • 相关阅读:
    隐式类型转换
    STL::allocator rebind
    Proxy Class(代理类)
    C++ 没有合适的默认构造函数(无参数构造函数)
    E
    C
    Multiplication Puzzle POJ
    Brackets POJ
    Halloween Costumes LightOJ
    ACwing 139. 回文子串的最大长度(二分+Hash)
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4414712.html
Copyright © 2011-2022 走看看