Spring 的ClassPathXmlApplicationContext()路径读取错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lucia_fanchen/article/details/47777473

  最近在学习Spring,由于所用的IDE是intellij idea,而网上的资料基本都是MyEclipse,在刚开始创建项目,特别是写第一个项目的时候得需要自己好好探索一番。。。(回归正题^_^)
// ClassPathXmlApplicationContext 是读取 src 目录下的配置文件
ApplicationContext  app=new ClassPathXmlApplicationContext(“application.xml”);

阅读更多
换一批

springClassPathXmlApplicationContext

02-26

我想用dom4j模拟一下spring的ioc,可是最后一句总空指针 是怎么回事rnpublic ClassPathXmlApplicationContext() throws Exception rn SAXReader reader = new SAXReader();rnrn // 构造文档对象rn Document document = reader.read(new File("src/config/spring/demo0/springMethod/beans.xml"));rnrn Element rootElmt = document.getRootElement();// 获取根元素beansrn List list = rootElmt.elements("bean");// 取名字为bean的所有元素rn for (int i = 0; i < list.size(); i++) rn Element element = (Element) list.get(i);rn Attribute attr = element.attribute("id");// 通过id找到这个属性rn// System.out.println(attr.getName() + "=" + attr.getValue());//通过找到的属性的getName()和getValue()找到属性和值rn String clazz = element.attributeValue("class");//通过class找到属性值rn Object o = Class.forName(clazz).newInstance();rn System.out.println(clazz);rn beans.put(attr.getName(), o);rn rn for (Element propertyElement : (List) element.elements("property")) rn String name = propertyElement.attributeValue("name"); // userDAOrn String bean = propertyElement.attributeValue("bean"); // urn Object beanObject = beans.get(bean);// UserDAOImpl instancernrn String methodName = "set" + name.substring(0, 1).toUpperCase()rn + name.substring(1);rn System.out.println("method name = " + methodName);rnrn System.out.println(bean);rn rn Method m = o.getClass().getMethod(methodName,beanObject.getClass().getInterfaces()[0]);rn m.invoke(o, beanObject);rn rnrn rn rn rnrnxml文件:rnrn rn rn rn rnrn

高分springClassPathXmlApplicationContext问题

07-26

我把applicationContex.xml放在WEB-INF下面,用ClassPathXmlApplicationContext (rn "/applicationContext-common-business.xml");rn rnClassPathXmlApplicationContext (rn "/WEB-INF/applicationContext-common-business.xml");rnrnClassPathXmlApplicationContext (rn "applicationContext-common-business.xml");rnrn都老是说找不到文件,怎么办.是不是要设置什么rn错误如下:rnrnrnorg.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext-common-business.xml,/applicationContext-portal.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext-common-business.xml,/applicationContext-portal.xml] cannot be opened because it does not existrnjava.io.FileNotFoundException: class path resource [applicationContext-common-business.xml,/applicationContext-portal.xml] cannot be opened because it does not existrn at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:127)rn at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:144)rn at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:99)rn at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:102)rn at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:70)rn at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:87)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:264)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:80)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:56)rn at com.guanghua.AppContext.(AppContext.java:31)rn at com.guanghua.AppContext.getInstance(AppContext.java:25)

没有更多推荐了,返回首页