Spring IOC与Bean容器
文章目录
什么是IOC
IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护
DI(依赖注入):一种实现方式
目的:创建对象并且组装对象之间的关系
Bean容器初始化
基础包:
- org.springframework.beans
- org.springframework.context
- BeanFactory提供配置结构和基本功能,加载并初始化Bean
- ApplicationContext保存了Bean对象并在Spring中被广泛使用
ApplicationContext方式
-
本地文件
//绝对路径 FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/workspace/appcontext.xml"); -
Classpath
//相对路径 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); -
Web应用中依赖servlet或Listener
<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>
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论