无法在Spring Java配置中使用ClassPathResource定位xml文件
我正在将项目从基于xml的配置迁移到基于java的配置 我能够成功地定义所有bean,只有一个除外。 XMLViewResolver 我们正在使用JasperReports,因此所有jrxml文件都在reports.xml中定义 在我的类路径中,我无法找到xml文件 我只尝试在ClassPathResource中添加reports.xml,但在项目构建期间仍然出现错误无法在Spring Java配置中使用ClassPathResource定位xml文件,java,spring,classpath,filenotfoundexception,Java,Spring,Classpath,Filenotfoundexception,我正在将项目从基于xml的配置迁移到基于java的配置 我能够成功地定义所有bean,只有一个除外。 XMLViewResolver 我们正在使用JasperReports,因此所有jrxml文件都在reports.xml中定义 在我的类路径中,我无法找到xml文件 我只尝试在ClassPathResource中添加reports.xml,但在项目构建期间仍然出现错误 @Bean public ViewResolver xmlViewResolver() { logger.in
@Bean
public ViewResolver xmlViewResolver() {
logger.info("xmlViewResolver");
XmlViewResolver bean = new XmlViewResolver();
bean.setOrder(10);
bean.setLocation(new ClassPathResource("WEB-INF/spring/appServlet/reports.xml"));
return bean;
}
旧xml代码:
<beans:bean id="viewResolver"
class="org.springframework.web.servlet.view.XmlViewResolver">
<beans:property name="order" value="0" />
<beans:property name="location"
value="/WEB-INF/spring/appServlet/reports.xml" />
</beans:bean>
我不得不将reports.xml移动到src/main/resources,并更改了位置,如下所示
bean.setLocation(new ClassPathResource("reports.xml"));
现在,项目能够读取和检测.xml文件
它不在类路径上(其中也有.class文件),而是网站上WEB\u INF目录中的一个实际文件。将reports.xml放在src/main/resources下的某个位置,并使用其中的路径作为类路径resource@JoopEggen,谢谢你的回复。即使使用“spring/appServlet/reports.xml”,它也显示了由以下原因引起的相同错误:java.io.FileNotFoundException:class path resource[spring/appServlet/reports.xml]谢谢。一旦我将reports.xml移到src/main/resources,并将位置更改为bean.setLocation(新类路径资源(“reports.xml”);,现在开始工作了。
bean.setLocation(new ClassPathResource("reports.xml"));