Spring Can';我得不到春豆

Spring Can';我得不到春豆,spring,spring-ioc,Spring,Spring Ioc,我有一个Spring项目,我想在我的SpringbeansXML文件中定义一个特定的Springbean。我的SpringBeanXML文件位于/WEB-INF/Spring/root-context.XML 以下是我在服务类中的代码: ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/spring/root-context.xml"); 以下是编译时出现的错误: parsing XML docu

我有一个Spring项目,我想在我的SpringbeansXML文件中定义一个特定的Springbean。我的SpringBeanXML文件位于
/WEB-INF/Spring/root-context.XML

以下是我在服务类中的代码:

ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/spring/root-context.xml");
以下是编译时出现的错误:

parsing XML document from class path resource [WEB-INF/spring/root-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-context.xml] cannot be opened because it does not exist

可能WEB-INF不在类路径中。我建议在类路径中移动xml文件(例如
src/main/resources/spring/root-context.xml
)。然后您可以通过以下方式访问它:
ApplicationContext context=new ClassPathXmlApplicationContext(“/spring/root-context.xml”)如果您在web应用程序中,SpringMVC将在
web-INF/-servlet.xml

如果要从WEB-INF访问上下文,可以使用

  GenericApplicationContext ctx = new GenericApplicationContext();
  XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
  xmlReader.loadBeanDefinitions(new FileSystemResource(path));

更好的方法是使用
WebApplicationContextUtils
或实现
ApplicationContextAware
。不确定您的用例是什么…

是的,我知道,但我想要的是,可以使用ClassPathXmlApplicationContext从MVC层和简单服务层类访问web inf中的XML文件。有办法做到这一点吗?