标签:system 方式 cto context sys resources test ffffff contex
一、ClassPathXmlApplicationContext 的具体路径
String s[] = System.getProperty("java.class.path").split(";");
for (String string : s) {
System.out.println(string);
}
排除掉lib以及C盘的,最后指向的是 ../target/classes/
所以把spring的xml放到main/resources下,Build-->make project(ctrl+F9)一下,即可在target生成,这时候使用以下语句就可以了
ApplicationContext bf = new ClassPathXmlApplicationContext("applicationContext.xml");
二、ClassPathXmlApplicationContext 使用方法
public void testBean(){ //单配置文件方式一 BeanFactory beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml"); //单配置文件方式二 BeanFactory beanFactory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); //多个配置文件 BeanFactory beanFactory=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); //绝对路径需加“file:”前缀 BeanFactory beanFactory = new ClassPathXmlApplicationContext("file:E:\Workspace\idea_workspace\spring\springtest\src\main\resources\applicationContext.xml"); TestBean bean= (TestBean) beanFactory.getBean("testBean"); assertEquals("testStr",bean.getTestStr()); }
三、FileSystemXmlApplicationContext使用方法
public void testBean(){
//项目根目录 BeanFactory beanFactory=new FileSystemXmlApplicationContext("applicationContext.xml");
//classes目录 BeanFactory beanFactory=new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
//项目路径相对路径 BeanFactory beanFactory=new FileSystemXmlApplicationContext("src\\main\\resources\\applicationContext.xml"); //多配置文件 BeanFactory beanFactory=new FileSystemXmlApplicationContext(new String[]{"src\\main\\resources\\applicationContext.xml"}); //绝对目录 BeanFactory beanFactory=new FileSystemXmlApplicationContext(new String[]{"E:\\Workspace\\idea_workspace\\spring\\springtest\\src\\main\\resources\\applicationContext.xml"}); TestBean bean= (TestBean) beanFactory.getBean("testBean"); assertEquals("testStr",bean.getTestStr()); }
标签:system 方式 cto context sys resources test ffffff contex
原文地址:http://www.cnblogs.com/wdw31210/p/7739488.html