一. IOC及Bean容器#
1. 什么是IOC
IOC:控制反转,控制权的转移,应用程序本身不负责以来对象的创建和维护,而是由外部容器负责创建和维护。
DI(依赖注入)是其一种实现方式。
目的:创建对象并且组装对象之间的关系
2. Bean容器初始化
-
基础:两个包
-org.springframework.beans
-org.springframework.context
-BeanFactory提供配置结构和基本功能,加载并初始化Bean
-ApplicationContext保存了Bean对象并在Spring中被广泛使用 -
方式,ApplicationContext
-本地文件
-Classpath
-Web应用中以来servlet或Listener -
文件
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/workspace/appcontext.xml");
- Classpath
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
- Web应用
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
网友评论