我们项目经理把Spring的配置文件放在了WEB-INF下,不让我们改路径,Spring的配置文件的名字是applicationContext.xml,我们这是web项目
我想用这个方法BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");Tomcat启动时就报错,无论我改成ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml"),ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml"),
ClassPathXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml"),ClassPathXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml"),都报错,
报的是类似这个错严重: Error configuring application listener of class com.pdics.listener.ReceiveData
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/applicationContext.xml] cannot be opened because it does not exist
------解决方案--------------------
是路径的问题 如果你的applicationContext.xml放在了WEB-INF下的话 那不如用相对路径
即写成
Resource resource = new ClassPathResource("../applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
应该就可以了