为什么我的Web应用程序应用程序上下文无法加载我的jar属性文件?

时间:2014-08-07 20:01:55

标签: spring-mvc classloader spring-boot

这是我的设置/上下文。我有一个使用spring-boot 1.1.4的JAR项目,它使用Java配置加载属性文件:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(name="appProps", value="classpath*:application-${spring.profiles.active}.properties")
public class DataJpaApplication {


@Autowired Environment env;


@Bean
//note, this PSPSHC is ours derived from the Spring one with specialized code.
public static org.springframework.context.support.PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceHolderConfigurer encryptionPropertySourcesPlaceHolderConfigurer = new PropertySourcesPlaceHolderConfigurer();
    encryptionPropertySourcesPlaceHolderConfigurer
            .setIgnoreUnresolvablePlaceholders(true);
    return encryptionPropertySourcesPlaceHolderConfigurer;
}
}

这个jar可以单独使用单元测试。然后我尝试将它合并到一个Web应用程序(不使用spring-boot的spring mvc web app)中,使其成为maven依赖项并将其添加到上下文中:

<beans:bean id="jpaConfigBean"
    class="com.somepackage.DataJpaApplication" />

我有一个Web应用程序单元测试,它尝试加载Web应用程序的应用程序上下文,并且工作正常(包括从jar加载上下文和属性文件)。但是,当我将此Web应用程序部署到容器(tcServer)时,它会失败并显示以下内容:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/application-local.properties]

修改

关于上述错误的澄清。 Web应用程序正在加载其上下文并导入jar文件的上下文。正是这个jar项目通过@PropertySource加载application-local.properties。那么,这可能是围绕父/子上下文交互吗?

注意:否代码在哪里添加斜杠。

以下是我在网络应用程序中引导spring配置的方法:

<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:**/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

我检查过以下几类:

  1. 确认maven生成的清单在war文件中有一个jar文件的类路径条目。
  2. 更改web.xml以使用/WEB-INF/servlet-context.xml(标准路径)
  3. 在JBoss中部署此Web应用程序而不是tc
  4. 将应用程序 - *。属性文件移动到另一个文件夹并引用该文件夹
  5. 将* .property文件复制到Web应用程序项目
  6. 确认WTP部署程序集实际上是引用maven
  7. 打开生成的war文件并确认jar文件中的文件位于预期位置。
  8. 以及其他一些没有成功的事情。

    我没有看到这个问题。请帮助找出错误/错误配置的位置。另外,如果您需要查看其他配置或文件以帮助调试此问题,请与我们联系。

    提前致谢,

    斯科特

    修改

    完成web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    
      <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>log4j.xml</param-value>
       </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
     <!-- commented out for debugging...trying to reduce the complexity to get to root cause
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:**/*Context.xml,     
            classpath*:**/*-context.xml
         </param-value>
      </context-param> -->
      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath*:**/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>
      <filter>
        <filter-name>springOpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
      </filter>
      <filter-mapping>
       <filter-name>springSecurityFilterChain</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
     <filter-mapping>
        <filter-name>springOpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
      <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher
      </listener-class>
    </listener>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/404.jsp</location>
    </error-page> 
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/views/500.jsp</location>
    </error-page>
    <error-page>
     <location>/WEB-INF/views/500.jsp</location>
    </error-page>  
    </web-app>
    

    完成(混淆)servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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.xsd 
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    
        <beans:bean id="jpaConfigBean"
            class="com.somepackage.DataJpaApplication" />
    
    
        <!-- DispatcherServlet Context: defines this servlet's request-processing 
            infrastructure -->
    
        <context:annotation-config />
        <context:component-scan base-package="com.somepackage.*" />
    
        <!-- Needed for transaction methods in controllers -->
        <tx:annotation-driven />
    
        <beans:bean id="ehCacheManager"
            class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
            p:shared="true" />
    
        <beans:bean id="cacheManager"
            class="org.springframework.cache.ehcache.EhCacheCacheManager"
            p:cacheManager-ref="ehCacheManager" />
    
        <cache:annotation-driven cache-manager="cacheManager" />
    
        <task:scheduled-tasks>
            <task:scheduled ref="runScheduler" method="run" cron="0 0 3 * * *" />
            <task:scheduled ref="runScheduler" method="run"
                initial-delay="0" fixed-rate="#{ T(java.lang.Long).MAX_VALUE }" />
        </task:scheduled-tasks>
    
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <annotation-driven conversion-service="conversionService" />
    
    
        <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
            up static resources in the ${webappRoot}/resources directory -->
        <resources mapping="/resources/**" location="/resources/" />
    
        <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
            in the /WEB-INF/views directory -->
        <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
    
        <beans:bean id="validator"
            class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    
        <beans:bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    
        <beans:bean id="contentNegotiationManager"
            class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
            <beans:property name="favorPathExtension" value="true" />
            <beans:property name="favorParameter" value="false" />
            <beans:property name="ignoreAcceptHeader" value="false" />
            <beans:property name="mediaTypes">
                <beans:value>
                    html=text/html
                    json=application/json
                    xml=application/xml
                </beans:value>
            </beans:property>
        </beans:bean>
    
    </beans:beans>
    

    除了我上面提到的内容之外,我不确定这些文件中是否有任何相关内容,但是为了清晰起见,每个请求都添加了

0 个答案:

没有答案