0

I have been struggling for some time now to get the properties to work, however I am having no luck. I have two configuration XML files in my project, one is a servlet config and the other is the global 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>

So the property access above in the spring xml works fine because the property file is loaded within the same scope.

Here is the web.xml where I'm loading the 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>

However, I have a bean in the servlet scope where I'm trying to use @Value but it's always 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;
    }
}

}

Environment is null when I debug, same as both string properties. Maybe I am not using the code correctly but is there a way to share properties between the parent and servlet?

P.S I know that there's a way to provide a default value in the annotation, but please ignore the fact that I did it myself manually.

Jeremy Reed
  • 289
  • 7
  • 20

3 Answers3

0

You should:

a. Add the default value, and

b. Your default value is a URL containing a colon(:). Use single quotes for this URL value (or leave it based on your spring version. See: 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;

or for 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;
Community
  • 1
  • 1
0

seems you have not loaded servlet context properly,

change,

<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>
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
0

Turns out the issue was due to using the wrong property holder configurer in a dependency module I wrote. I was using ServletContextPropertyPlaceholderConfigurer and instead needed to use PropertySourcesPlaceholderConfigurer.

Jeremy Reed
  • 289
  • 7
  • 20