Spring @Value注入值失败,错误信息提示:Could not resolve placeholder

Spring @Value注入值失败,错误信息提示:Could not resolve placeholder

问题根源:

@Value("${wx.app.config.appid}")
public Object appid;

异常信息:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'wx.app.config.appid' in string value "${wx.app.config.appid}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:823)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1084)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    ... 79 more

解决方法:

Spring引入.properties文件的两种方法(如下)

1.通过xml方式加载.properties文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">   
        <array>  
            <value>classpath:conn.propertiesvalue>  
        array>  
    property>  
bean>  

2.通过注解方式加载.properties文件

<context:ignore-unresolvable="true" location="classpath:config/**/*.properties" system-properties-mode="OVERRIDE"/>

知道了怎么配置,就知道自己有没有配置错误,如果配置没有问题,那么就继续找问题,Could not resolve placeholder占位符不能被解析,那就是没有在.properties中找到要注入的值了,原来是在xml里面配置了2个ignore-unresolvable,这就是定位问题所在了。


<context:property-placeholder location="classpath*:/mybatis/db.properties"/>

<context:property-placeholder ignore-unresolvable="true" location="classpath:config/config-wxapp.properties" ignore-resource-not-found="true"/>

一个是引入配置文件.properties,一个是引入db配置文件.properties,其实配置多个并不是报错的主要问题所在,是因为spring的加载机制,原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了),所以根据加载的顺序,配置的第二个property-placeholder就被没有被spring加载,我想引入的config-wxapp.properties就没有被引入,所以在使用@Value注入的时候占位符就解析不了...解决方法就是把2个property-placeholder注解配置合并在一起就好了


<context:property-placeholder ignore-unresolvable="true" location="classpath*:config/config-*.properties,classpath*:mybatis/db.properties" ignore-resource-not-found="true"/>

 

posted @ 2018-01-12 10:02 dawn-tangzedong 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(Spring @Value注入值失败,错误信息提示:Could not resolve placeholder)