Spring IoC容器概述
本节示例代码: think-in-spring/ioc-container-overview
根据<<小马哥讲Spring编程思想>>,学习笔记总结
一、Spring IoC依赖查找
-
根据名称查找
-
实时查找
-
延时查找
- FactoryBean
-
-
根据类型查找
- 单个类型查找
- 集合类型查找
-
根据注解方式查找
二、 Spring IoC依赖注入
-
根据Bean名称注入
-
根据BeanType注入
- 单个Bean对象
- 集合Bean对象
-
注入容器内建Bean对象
-
注入非Bean对象
-
注入类型
- 实时注入
- 延时注入
三、Spring IoC 依赖来源
- 自定义Bean
- 容器内建Bean对象
- 容器内建依赖
四、 Spring IoC配置元信息
-
Bean定义配置
- 基于XML文件
- 基于Properties文件
- 基于Java注解
- 基于Java API (专题讨论)
-
IoC容器配置
- 基于XML文件
- 基于Java注解
- 基于Java API (专题讨论)
-
外部化属性配置
- 基于Java注解
五、 Spring IoC 容器
- BeanFactory 和 ApplicationContext 谁才是Spring IoC容器?
六、Spring 应用上下文
- ApplicationContext 除了IoC容器角色,还提供了哪些功能?
- 面向切面 (AOP)
- 配置元信息(Configuration Metadata)
- 资源管理(Resources)
- 事件(Events)
- 国际化(i18n)
- 注解(Annotations)
- Environment 抽象(Environment Abstraction)
七、 使用 Spring IoC容器
-
BeanFactory 是 Spring 底层 IoC容器
-
DefaultListableBeanFactory
//1. 创建容器 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactroy(); //2. 加载配置 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(); //2.1 String location = "classpath:META-INF/applicationContext.xml"; reader.loadBeanDefinitions(location);
-
ApplicationContext 是具备应用特性的BeanFactory 超集
- AnnotationApplicationContext
AnnotationApplicationContext applicationContext = new AnnotationApplicationContext(); applicationContext.register(class); User user = applicationContext.getBean(User.class); // 可以获取到User对象信息 @Bean public User user(){ User user = new ("1","howliked"); return user; }
- AnnotationApplicationContext
八、Spring IOC容器生命周期
-
启动
- applicationContext.refresh();
-
运行
-
停止
- applicationContext.close();
九、面试题精选
-
沙雕面试题 - 什么是Spring IoC容器?
-
996面试题 - BeanFactory 与 FactoryBean的区别?
- BeanFactory是Spring IoC 最底层的容器
- FactoryBean是创建Bean的一种方式,帮助实现复杂的初始化逻辑
FactoryBean创建Bean是否会执行Bean的生命周期?
-
劝退面试题 - Spring Ioc容器启动时做了哪些准备?
-