我打算在我的 Spring MVC 应用程序中做一些事情。
- 使用注释在我的
ConfigFactory类中加载带有配置的静态 .xml 文件。@Service - 做一些解析并创建一个
Configuration类的实例。 - 将此类传递
Configuration给AnotherServiceClass它将创建一个model我想稍后传递给view.
此静态文件位于“WEB-INF/config/sampleItem.xml”位置。
当我尝试在 @Controller 中执行第一步ItemController时,我对此没有任何问题,因为我可以访问ServletContext:
@Autowired ServletContext servletContext;
public ModelAndView method(){
File xmlFile = new File(servletContext.getRealPath("/WEB-INF/config/sampleItem.xml"));
Document config = new SAXBuilder(XMLReaders.NONVALIDATING).build(xmlFile);
...
}
但我不知道如何在我的@Service classes. 当我ServletContext在我的构造函数中尝试@AutowiringConfigurationFactory并再次在它的构造函数中加载 XML 时:
File xmlFile = new File(servletContext.getRealPath("/WEB-INF/config/sampleItem.xml"));
我最终遇到了一个例外:
Error creating bean with name 'itemController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.package.service.configuration.ConfigFactory org.package.controller.ItemController.configFactory; (...)