例如配置文件如下
<beans>
<bean id="a" class="......A">
.............
</bean>
</beans>
第一种方法
BeanFactory container=
new XmlBeanFactory(new ClassPathResource("配置文件路径"));
A a=(A)container.getBean("a");
第二种方法
ApplicationContext container=
new ClassPathXmlApplicationContext("配置文件路径"); //从类路径中搜索XML配置文件
A a=(A)container.getBean("a");
第三种方法
ApplicationContext container=
new FileSystemXmlApplicationContext("配置文件路径"); //通过绝对或相对路径制定XML配置文件
A a=(A)container.getBean("a");
本文介绍了使用Spring框架通过三种不同的方式实例化配置文件中的bean。包括利用XmlBeanFactory从类路径资源加载配置文件、使用ClassPathXmlApplicationContext从类路径搜索配置文件及FileSystemXmlApplicationContext通过指定的绝对或相对路径加载配置文件。

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



