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