Spring MVC アプリでいくつかのことを計画しています。
- 注釈付きの
ConfigFactoryクラスに構成を含む静的 .xml ファイルをロードします。@Service - いくつかの解析を行い、
Configurationクラスのインスタンスを作成します。 - この
Configurationクラスを渡して、後で に渡したいAnotherServiceClassを作成します。modelview
この静的ファイルは、'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。@Autowiring を試してみServletContextて、ConfigurationFactoryコンストラクターに 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; (...)