java.io.FileNotFoundException:无法打开ServletContext资源[/resources/properties/database.properties]

时间:2014-11-18 21:40:23

标签: java eclipse spring hibernate java-ee

我现在使用spring和Java EE构建一个Web应用程序,但我有一个例外。我尝试从属性文件中读取值。我在resources文件夹中有一个属性文件夹,而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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

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

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

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