1、配置文件的命名
Spring框架中的默认配置文件,建议命名为applicationContext.xml
* 编写配置文件,默认位置有两个 ①src目录、②WEB-INF目录
2、Spring 配置文件分离 第一种 new ClassPathXmlApplicationContext("bean1.xml","bean2.xml"); 同时加载多个配置文件,这些配置文件 并列关系 第二种 new ClassPathXmlApplicationContext("applicationContext.xml"); 在applicationContext.xml <import resource="classpath:bean1.xml"/> <import resource="classpath:bean2.xml"/> 主配置文件是 applicationContext.xml ,在主配置文件中 引入 子配置文件 bean1.xml bean2.xml 在开发中 主要用 第二种
3、读取配置文件
读取配置文件 在开发中
1 // spring内部提供工厂,只需要将实现类进行配置,交由Spring工厂创建对象2 // 读取 Spring配置文件3 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(4 "applicationContext.xml");5 6 // applicationContext就是工厂7 // 通过配置bean的id获得class类实例8 IHelloService helloService2 = (IHelloService) applicationContext9 .getBean("helloService");
ApplicationContext应用上下文,加载Spring框架
加载classpath:
new ClassPathXmlApplicationContext("applicationContext.xml");
加载磁盘路径:
new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
4、BeanFactory 接口 和 ApplicationContext 接口关系
AplicationContext 是 BeanFactory 的 一个子接口
BeanFactory 是Spring 核心工厂接口,加载Bean 采用延迟加载,使用getBean时,才会创建 Bean的实例
ApplicationContext 接口 对BeanFactory 的一个扩展 ,默认在加载配置文件时,就会创建Bean实例 ,同时提供额外功能
* 国际化处理
* 自动装配
* 事件传递
* 各种不同应用层的Context实现
* 在实际开发中,通常使用 ApplicationContext 接口