Spring Properties文件没有自动连线,@ Value值始终为NULL

时间:2017-10-03 14:28:26

标签: java spring rest web-services

我有一个Spring Web应用程序(RestFul Web服务) 我在applicationContext.xml中有以下内容来读取属性文件:

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="properties" ref="appProperties" />
</bean>

<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:common.properties</value>
            <value>classpath:local.properties</value>
            <value>classpath:${XPLAT_ENV}.properties</value>
        </list>
    </property>
</bean>

我的Rest API就像这样:

@Path("/login")
@Component
public class CLSAmple {

    @Autowired
    @Qualifier("appProperties")
    protected Properties appProperties;


    @Value("${ui.server.endpoint}")
    private String uiservername;


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getLoginInformation() {
        if(appProperties != null){
            System.out.println("AppProperties is NOT null");
            String uiServer = appProperties.getProperty("ui.server.endpoint");
            return uiServer;
        }
        System.out.println("AppProperties is NULL");
        System.out.println("@Value  is " + uiservername);
        return "Hello Java!!";
    }

}

但我的@Autowired appProperties@Value获取特定属性始终为NULL。我已经查看了有关从属性文件中注入值的类似帖子,并尝试了所有可能的建议,但它仍然是NULL。 还尝试了Annotation只使用方法 @PropertySource("classpath:common.properties") 但结果相同。

由于

已编辑:这是applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:metrics="http://www.ryantenney.com/schema/metrics" 
 xmlns:util="http://www.springframework.org/schema/util"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics.xsd">

<context:component-scan base-package="com.cl.connect" />

<context:annotation-config/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="properties" ref="appProperties" />
</bean>

<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:common.properties</value>
        </list>
    </property>
</bean>
</beans>

1 个答案:

答案 0 :(得分:0)

我发现了问题所在,就像上面评论中提到的那样, 我的集成有误,我在web.xml中添加了以下内容并且有效:

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>