【问题标题】:Spring @Value access to property loaded in parent contextSpring @Value 访问在父上下文中加载的属性
【发布时间】:2017-03-29 23:49:10
【问题描述】:

我一直在努力让这些属性正常工作,但是我没有运气。我的项目中有两个配置xml文件,一个是servlet config,另一个是全局spring config xml。

Servlet 配置 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>

弹簧配置 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>

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

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 我知道有一种方法可以在注释中提供默认值,但请忽略我自己手动完成的事实。

【问题讨论】:

  • 你是如何加载上下文的?
  • 我用我的 web.xml 更新了

标签: java xml spring spring-mvc servlets


【解决方案1】:

你应该:

一个。添加默认值,然后

b.您的默认值是包含冒号 (:) 的 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:

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

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

【讨论】:

    【解决方案2】:

    看来你没有正确加载 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>
    

    【讨论】:

    • servlet 的名称与 xml 文件的名称匹配,因此无需定义 init-param
    【解决方案3】:

    原来问题是由于在我编写的依赖模块中使用了错误的属性持有者配置器。我使用的是 ServletContextPropertyPlaceholderConfigurer,而需要使用 PropertySourcesPlaceholderConfigurer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2014-12-23
      • 2018-10-15
      • 2020-06-21
      • 2013-08-31
      相关资源
      最近更新 更多