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();
}

本文介绍了Spring框架中两种常见的配置文件加载方式:通过ClassPathXmlApplicationContext加载类路径下的配置文件,以及通过FileSystemXmlApplicationContext加载磁盘上的配置文件。通过示例展示了如何使用这两种方式来初始化Spring的应用上下文。
9699

被折叠的 条评论
为什么被折叠?



