在使用Spring MVC過程中,部署項目報錯,報錯信息如下:
八月 15, 2016 5:02:04 下午 org.apache.catalina.core.StandardContext listenerStart
嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/db.properties]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
.......
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/db.properties]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:153)
... 51 more
...............................................................................................................................................................................
Maven項目,application-context.xml、db.properties文件均放置在src/main/resources目錄下,Tomcat部署項目,src/main/resources目錄下的配置文件默認位置為:{項目名}/WEB-INF/classes,而Spring卻在項目根目錄下尋找,肯定找不到,因此,配置時指定classpath目錄下尋找即可。
解決方案如下:
<context:property-placeholder location="classpath:db.properties" />