【问题标题】:java.io.FileNotFoundException: Could not open ServletContext resource [/resources/properties/database.properties]java.io.FileNotFoundException:无法打开 ServletContext 资源 [/resources/properties/database.properties]
【发布时间】:2015-01-16 05:56:42
【问题描述】:

我现在使用 Spring 和 Java EE 构建了一个 Web 应用程序,但我遇到了一个例外。我尝试从属性文件中读取值。我在资源文件夹中有一个属性文件夹,servlet-context.xml 位于 WEB-INF/spring/appServlet/

<context:property-placeholder location="resources/properties/database.properties" /><context:component-scan base-package="com.journaldev.spring" />    

我尝试了使用类路径的另一种替代方法,但没有:

<context:property-placeholder location="classpath:resources/properties/database.properties" />

当我运行 apache tomcat 时:

Nov 18, 2014 10:28:52 PM org.apache.catalina.core.StandardContext loadOnStartup
SCHWERWIEGEND: Servlet /spring threw load() exception
java.io.FileNotFoundException: Could not open ServletContext resource [/resources/properties/database.properties]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
    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.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:142)
    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:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:4007)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:425)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1546)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1556)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1556)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1524)
    at java.lang.Thread.run(Thread.java:745)

Nov 18, 2014 10:28:52 PM org.apache.catalina.core.StandardContext reload
INFORMATION: Reloading Context with name [/spring] is completed

有什么想法吗?

【问题讨论】:

    标签: java eclipse spring hibernate jakarta-ee


    【解决方案1】:

    您是否使用 maven 从源代码构建您的战争文件?如果是这样,那么我假设你上面提到的资源目录在你的 maven 项目的 src 目录下。

    对于这种情况,您需要在 spring 上下文 xml 中具有以下声明:

    <context:property-placeholder location="classpath:properties/database.properties" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2023-03-15
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 2022-01-18
      相关资源
      最近更新 更多