方法一:从classpath路径加载
在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
可以在整个类路径中寻找xml文件
* 通过这种方式加载。需要将spring的配置文件放到当前项目的classpath路径下
* classpath路径指的是当前项目的src目录,该目录是java源文件的存放位置。
方法二:从磁盘路径加载
在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\applicationContext.xml”});
- Spring的配置文件可以指定多个,可以通过String数组传入。
(2)通过getBean方法获得Spring容器管理Bean对象
如何选择:
- 如果applicationContext.xml 在 src下, ClassPathXmlApplication读取
- 如果applicationContext.xml 在WEB-INF下,FileSystemXmlApplicationContext读取