2016-11-15 61 views
0

我一直在努力一段時間來獲得屬性的工作,但我沒有運氣。我的項目中有兩個配置XML文件,一個是servlet配置,另一個是全局彈簧配置xml。Spring @Value訪問屬性在父上下文中加載

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> 

Spring配置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範圍在那裏我試圖用@Value豆,但它總是空。

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我知道有一種方法可以在註釋中提供默認值,但請忽略我自己手動完成的事實。

+1

你是如何加載上下文的? – kuhajeyan

+0

我使用我的web.xml更新了 –

回答

0

原來的問題是由於使用了錯誤的PR我寫的依賴模塊中的operty持有者配置器。我正在使用ServletContextPropertyPlaceholderConfigurer,而需要使用PropertySourcesPlaceholderConfigurer。

0

您應該:

a。添加默認值,並且

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; 

或春季4.2:

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

@Value("${registry.errorserviceurl:http://localhost:8090/registry/rest/notifyError}") 
String registryErrorUrl; 
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> 
+0

該servlet的名稱與xml文件的名稱相匹配,因此無需定義init-param –