spring中applicationContext

来源:互联网 发布:网络女主播直播换衣服 编辑:程序博客网 时间:2024/06/05 16:12
如果说BeanFactory是spring的心脏,那么Application就是完整的身躯。ApplicationContext就是由BeanFactory派生出来的。

ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,

前者默认从类路径加载配置文件,后者默认从文件系统加载文件。

如果配置文件放在类路径下,直接使用ClassPathXmlApplicationContext实现类:
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/techman/context/beans.xml");
这里的参数等同于:"classpath:com/techman/context/beans.xml"
如果配置文件在文件系统的路径下,则可以优先考虑使用FileSystemXmlApplicationContext实现类:
ApplicationContext ctx=new FileSystemXmlApplicationContext("com/techman/context/beans.xml");
这里的参数等同于:"file:com/techman/context/beans.xml".
还可以指定一组配置文件,Spring自动将多个配置文件在内存中整合成一个配置文件:
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"conf/bean1.xml","conf/bean2.xml"});
原创粉丝点击