没有web.xml的ServletContextParameterFactoryBean

时间:2015-07-02 14:57:23

标签: spring spring-boot

我在项目中使用spring boot。在该项目中,我需要从另一个项目导入applicationContext.xml,如下面的代码:

@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具有以下方面:

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

现在,我想在不使用带有以下内容的web.xml的情况下定义propertiesLocation:

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

我尝试了所有我找到但没有成功的解决方案(例如How to set context-param in spring-boot)。当我构建项目时,它总是抱怨缺少的propertiesLocation。是否有任何解决方案不涉及web.xml或对applicationContext.xml的修改?

当我尝试执行“mvn spring-boot:run”时,它失败并出现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 个答案:

答案 0 :(得分:0)

不要使用ServletContextParameterFactoryBean。较新版本的spring使用PropertySourcePlaceholderConfigurer。因此,请使用@Value("${propertiesLocation}"),这取决于位置将使用servlet上下文或不查找属性。因此,如果您可以删除它,则增加的优势是您可以使用系统属性覆盖servlet上下文中的属性(或者在JNDI中定义它们)。

如果你真的想配置它,将它添加到application.properties就足够了。但我强烈建议你们一起去除豆子。

最终解决方案是使用@Bean方法简单地覆盖bean。哪个应该为您提供使用PropertySource的优势。

@Autowired
private Environment env;

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