Spring框架加载配置文件:
ApplicationContext应用上下文,加载Spring框架配置文件。它有两个子类:
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();
}