SSH读取applicationContext.xml配置文件测试hibernate方法

如果你的applicationContext.xml文件是放在WEB-INF下:

使用ClassPathXmlApplicationContext类!

ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");

ClassPathXmlApplicationContext这个类他最终会在WEB-INF/classes/目录下寻找,你项目最终部署后src下的东西都会部署在WEB-INF/classes/目录下,所以木找到!

使用ClassPathXmlApplicationContext这样读取:则出现以下异常,找不到指定文件神马的!

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [E:\mycodes\shop2\WEB-INF\applicationContext.xml]; nested exception is java.io.FileNotFoundException: WEB-INF\applicationContext.xml (系统找不到指定的路径。)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
	at gzqh.wh.test.TestProduct.main(TestProduct.java:18)
Caused by: java.io.FileNotFoundException: WEB-INF\applicationContext.xml (系统找不到指定的路径。)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:106)
	at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:108)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
	... 13 more



没办法,就是读不到,你写绝得路径都没用!

好吧,读取文件所在的绝对路径吧,使用FileSystemXmlApplicationContext类!

 ApplicationContext context=new FileSystemXmlApplicationContext("E:\\mycodes\\shop2\\WebRoot\\WEB-INF\\applicationContext.xml");

-----------------------------------------------------------------------------------------------------

	public static void main(String[] args) {
		
	 ApplicationContext context=new FileSystemXmlApplicationContext("E:\\mycodes\\shop2\\WebRoot\\WEB-INF\\applicationContext.xml");
	 
	 ProductDAO dao =(ProductDAO) context.getBean("ProductDAO");
	 
	 List<Product> list =dao.findAll();
		
		for (Product product : list) {
			
			System.out.println(product);
			
			System.out.println(product.getCode());
 			
		}

	}




你可能感兴趣的:(SSH读取applicationContext.xml配置文件测试hibernate方法)