2016-11-15 6 views
0

私は今、プロパティを動作させるためにしばらくの間苦労していますが、私は運がないです。私は自分のプロジェクトに2つの設定XMLファイルを持っています.1つはサーブレット設定で、もう1つはグローバルバネ設定XMLです。春@親のコンテキストにロードされたプロパティへの@Valueアクセス

サーブレットの設定の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> 

プロパティファイルは、同じ範囲内にロードされるため、だから、春の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を使用しようとしているが、それは常にnullのサーブレットスコープで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; 
    } 
} 

} 

両方の文字列のプロパティと同じように、デバッグすると環境はnullです。たぶん私はコードを正しく使用していませんが、親とサーブレットの間でプロパティを共有する方法はありますか?

P.Sアノテーションにデフォルト値を指定する方法はありますが、手動で自分で行ったことを無視してください。

+1

どのようにコンテキストを読み込みましたか? – kuhajeyan

+0

web.xmlで更新しました –

答えて

0

は問題が間違ったPRを使用する予定だったが判明私が書いた依存モジュールのオペレーターホルダー設定者。 ServletContextPropertyPlaceholderConfigurerを使用していて、代わりにPropertySourcesPlaceholderConfigurerを使用する必要がありました。

0

次のようにします。

a。デフォルト値を追加し、

b。デフォルト値は、コロン(:)を含むURLです。このURL値に一重引用符を使用します(または、あなたの春バージョンに基づいて、それを残してください:。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; 
0

あなたは、適切

をサーブレットコンテキストをロードしていないようです

変更、

<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

サーブレットの名前がxmlファイルの名前と一致するため、init-paramを定義する必要はありません –

関連する問題