Spring ClassPathXmlApplicationContext and FileSystemXmlApplicationContext使用说明

时间:2012-01-21 浏览:10次 来源:本站

spring提供了初始化应用上下文环境的配置文件,此外在程序中可以通过ClassPathXmlApplicationContext或FileSystemXmlApplicationContext类实现个性化的配置导入。

1. ClassPathXmlApplicationContext

导入XML从CLASSPATH路径中寻找XML文件并导入.

比如:

ApplicationContext classpathCtx = new ClassPathXmlApplicationContext("helloWorld.xml");

HelloWorld helloBean = (HelloWorld) classpathCtx.getBean("helloWorld");

2.FileSystemXmlApplicationContext

从文件路径中导入配置文件,需要一个可访问的文件全路径

比如:

FileSystemXmlApplicationContext fileCtx = new FileSystemXmlApplicationContext("C:\\helloWorldFile.xml");

HelloWorld helloBean = (HelloWorld) fileCtx.getBean("helloWorld");

∮推荐文章

↑阅读排行