Spring @Value访问父上下文中加载的属性

时间:2016-11-15 22:37:18

标签: java xml spring spring-mvc servlets

我一直在苦苦挣扎一段时间才能让这些物业运转起来,但是我没有运气。我的项目中有两个配置XML文件,一个是servlet配置,另一个是全局spring config xml。

Servlet config xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<task:annotation-driven />

<context:component-scan base-package="com.test.loadbalancer"></context:component-scan>

<mvc:annotation-driven />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

Spring config xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-4.3.xsd
        http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
    http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.1.xsd
    http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="false"/>
    <property name="locations">
      <list>
        <value>classpath:com/test/loadbalancer/config.properties</value>
      </list>
    </property>
</bean> 

<rabbit:connection-factory id="connectionFactory" host="${RABBIT_MQ_HOST}" port="${RABBIT_MQ_PORT}" username="${RABBIT_MQ_USER}" password="${RABBIT_MQ_PASS}" />   <!-- This works fine -->
</beans>

因此,spring xml上面的属性访问工作正常,因为属性文件在同一范围内加载。

这是我正在加载xml的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Test Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value> 
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

但是,我在servlet范围内有一个bean,我正在尝试使用@Value,但它始终为null。

package com.test.loadbalancer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.test.micro.util.registry.NotificationMessage;

@Component
public class RegistryEndpointEnricher {

private static final String defaultRetrieveServiceURL = "http://localhost:8090/registry/rest/retrieveServiceInfo";
private static final String defaultErrorServiceURL = "http://localhost:8090/registry/rest/notifyError";

@Value("${registry.retrieveserviceurl}")
String registryRetrieveServicesUrl;
@Value("${registry.errorserviceurl}")
String registryErrorUrl;

@Autowired
private Environment env;

public String getEndpoint(NotificationMessage nmMessage) {
    populateURLs();
    String registryURl = null;
    switch (nmMessage.getNotificationType()) {
        case RETRIEVESERVICEINFO:
            registryURl = registryRetrieveServicesUrl;
            break;
        case ERROR:
            registryURl = registryErrorUrl;
            break;
        default:
            break;
    }
    return registryURl;

}

private void populateURLs() {
    if (StringUtils.isEmpty(registryRetrieveServicesUrl)) {
        registryRetrieveServicesUrl = defaultRetrieveServiceURL;
    }
    if (StringUtils.isEmpty(registryErrorUrl)) {
        registryErrorUrl = defaultErrorServiceURL;
    }
}

}

调试时环境为空,与两个字符串属性相同。也许我没有正确使用代码,但是有没有办法在父代和servlet之间共享属性?

P.S我知道有一种方法可以在注释中提供默认值,但请忽略我自己手动完成的事实。

3 个答案:

答案 0 :(得分:0)

你应该:

一个。添加默认值,

湾您的默认值是包含冒号(:)的URL。对此URL值使用单引号(或根据您的spring版本保留它。请参阅:Spring @Value escape colon(:) in default value

@Value("${registry.retrieveserviceurl:http://localhost:8090/registry/rest/retrieveServiceInfo}")
String registryRetrieveServicesUrl;

@Value("${registry.errorserviceurl:http://localhost:8090/registry/rest/notifyError}")
String registryErrorUrl;

或Spring 4.2:

arr_int = arr.astype('int64')

答案 1 :(得分:0)

似乎你没有正确加载servlet上下文,

变化,

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value> 
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>test-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>test-servlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

答案 2 :(得分:0)

原来问题是由于在我编写的依赖模块中使用了错误的属性holder configurer。我使用的是ServletContextPropertyPlaceholderConfigurer,而是需要使用PropertySourcesPlaceholderConfigurer。