2015-07-02 27 views
0

我在一個項目上使用spring啓動。在這個項目上我需要從另一個項目導入applicationContext.xml中,如下面的代碼:沒有web.xml的ServletContextParameterFactoryBean

@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}")); 
    } 
} 

一個上的applicationContext.xml豆的有以下方面:

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

現在,我要定義propertiesLocation不使用web.xml中有以下:

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

我想我找到了,但沒有sucess(例如的所有解決方案)。當我構建項目時,它總是抱怨缺少的propertiesLocation。是否有任何解決方案不涉及web.xml或對applicationContext.xml的修改?

當我嘗試做一個「MVN彈簧啓動:運行」時,出現一個拋出: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) 

,我使用爲指導的另一個項目使用下面的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> 
    ... 

所以,我想沒有web.xml中

+1

這兩個解決方案(在'application.properties'中使用'ServletContextInitializer'或設置'server.context-parameters')在您鏈接的問題中爲我工作。也許你可以分享一個重現問題的示例項目? –

回答

0

配置我的項目像上面不要使用ServletContextParameterFactoryBean。較新版本的彈簧使用PropertySourcePlaceholderConfigurer。所以改爲使用@Value("${propertiesLocation}"),這取決於位置將使用servlet上下文還是不查找屬性。所以如果你可以刪除它,增加的好處是你可以使用系統屬性來覆蓋servlet上下文中的屬性(或者在JNDI中定義它們)。

如果你真的想配置它,將它添加到application.properties應該就足夠了。但我強烈要求你們一起去除豆子。

最終的解決方案是簡單地用@Bean方法覆蓋bean。這應該給你使用PropertySource的優勢。

@Autowired 
private Environment env; 

@Bean 
public String configLocation() { 
    return env.getRequiredProperty("propertiesLocation"); 
} 
+0

更改applicationContext.xml上的bean定義是我的最後一個資源。我已經嘗試添加到application.properties,並且當我執行mvn clean install時,測試階段引發了一個beanCreationException,因爲找不到servletContext init參數propertiesLocation。 –

+0

是因爲init參數還是因爲沒有'ServletContext'而真的失敗?你可以將測試用例添加到你的問題中,並澄清這是關於測試用例而不是運行時應用程序...... –

+0

我修復了測試。應用程序中的運行時錯誤仍在繼續 –

相關問題