0

I´m using spring boot on a project. On that project I need to import an applicationContext.xml from another project, like the following code:

@SpringBootApplication
@EnableSwagger
@EnableEntityLinks
@ImportResource("classpath:applicationContext.xml")
public class MyApplication {

  public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
  }

  @Bean
  public CurieProvider curieProvider() {
    return new DefaultCurieProvider("pm", new UriTemplate("http://www.xpto.com/docs/pm/rels/{rel}"));
  }
}

One of the beans on applicationContext.xml has the following aspect:

<bean id="configLocation" class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
  <property name="initParamName">
    <value>propertiesLocation</value>
  </property>
</bean> 

Now, I want to define the propertiesLocation without using a web.xml with the following:

<context-param>
  <description>
  </description>
  <param-name>propertiesLocation</param-name>
  <param-value>file:/a/b/c/application.properties</param-value>
</context-param> 

I tried all the solutions that I found but without sucess (for instance How to set context-param in spring-boot). When I build the project, it always complain about the missing propertiesLocation. Is there any solution that does not involve a web.xml or modifications to the applicationContext.xml?

When I try to do a "mvn spring-boot:run", it fails with a IllegalArgumentException:

java.lang.IllegalArgumentException: Resource must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.core.io.support.EncodedResource.<init>(EncodedResource.java:82)
    at org.springframework.core.io.support.EncodedResource.<init>(EncodedResource.java:67)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.nsn.oss.pm.api.MyApplication.main(MyApplication.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
    at java.lang.Thread.run(Thread.java:724)

Another project that I'm using as guidance uses the following web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>portal</display-name>
    <context-param>
        <description></description>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/datasourceContext.xml
            classpath:/applicationContext.xml
            classpath:/aopContext.xml
            classpath:/mailContext.xml
        </param-value>
    </context-param>
    <context-param>
        <description>
        </description>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:/log4j.properties</param-value>
    </context-param>
    <context-param>
        <description>
        </description>
        <param-name>propertiesLocation</param-name>
        <param-value>file:/a/b/c/application.properties</param-value>
    </context-param>
    ...

So, I'm trying to configure my project like the above without the web.xml

Community
  • 1
  • 1
jose_p
  • 624
  • 1
  • 8
  • 12
  • 1
    Both solutions (using a `ServletContextInitializer` or setting `server.context-parameters` in `application.properties`) in the question you've linked to work for me. Perhaps you can share a sample project that reproduces the problem? – Andy Wilkinson Jul 02 '15 at 16:46

1 Answers1

0

Don't use the ServletContextParameterFactoryBean. Newer version of spring use a PropertySourcePlaceholderConfigurer. So instead use a @Value("${propertiesLocation}") which will depending on the location will use the servlet context or not to lookup the property. So if you can remove it, added advantage is that you could use system properties to override properties from the servlet context (or define them in JNDI for instance).

If you really want to configure it adding it to the application.properties should be enough. But I would strongly urge you to remove the bean all together.

A final solution is to simply override the bean with a @Bean method. Which should give you the advantage of using PropertySources.

@Autowired
private Environment env;

@Bean
public String configLocation() {
    return env.getRequiredProperty("propertiesLocation");
}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Changing the bean definition on applicationContext.xml is my last resource. I already tried adding to application.properties and when I do mvn clean install, the tests phase raises a beanCreationException because can´t find the servletContext init parameter propertiesLocation. – jose_p Jul 03 '15 at 10:13
  • Is it really failing because the init parameter or because there is no `ServletContext`? Can you add the test case to your question and clarify that this is regarding a testcase and not a runtime application... – M. Deinum Jul 03 '15 at 10:29
  • I fixed the tests. The runtime error in the application continues – jose_p Aug 05 '15 at 11:06