Spring学习-03:ApplicationContext加载配置文件的两种方式

Spring框架加载配置文件:

ApplicationContext应用上下文,加载Spring框架配置文件。它有两个子类:

Spring学习-03:ApplicationContext加载配置文件的两种方式_第1张图片

ClassPathXmlApplicationContext:加载classpath下面的配置文件

public void demo2(){
		//创建一个工厂类
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloService helloService=(HelloService)applicationContext.getBean("userService");
		helloService.sayHello();
	}

FileSystemXmlApplicationContext:加载磁盘下的配置文件

public void demo3(){
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext("/WebRoot/applicationContext2.xml");
		HelloService helloService=(HelloService)applicationContext.getBean("userService");
		helloService.sayHello();
	}



你可能感兴趣的:(Spring,Spring学习之路)