Spring Boot 部署到Tomcat 报错:Could not open ServletContext resource [/xx.properties]
报错信息:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/xx:properties]
项目中引入的地方:
@PropertySource("xx.properties")
说明:在使用 Spring Boot 内置Tomcat 运行时并没有问题,当部署到外部 Tomcat 中便出现这个异常。
原因:Maven项目,application-context.xml、xx.properties文件均放置在src/main/resources目录下,Tomcat部署项目,src/main/resources目录下的配置文件默认位置为:{basePath}/WEB-INF/classes,而Spring却在项目根目录下寻找,肯定找不到。
解决方案:配置时指定classpath路径
-
第一种:在 application-context.xml 中加入以下配置
<context:property-placeholder location=“classpath:db.properties” />
-
第二种:在引用的地方指定根目录
@PropertySource(“classpath:xx.properties”)