Spring课程笔记
目标
- IOC
- AOP
- 事务处理
How
- 框架设计思想
- 学习基础操作
- 学习案例
发展
- Spring 全家桶
- Spring FrameWork
- 来源于EJB
- 1.0 纯配置
- 2.0 引入注解
- 3.0 完全注解
- 底层原理来源于配置
- Spring 5 全面支持 JDK 8
- Spring Boot
- Spring Cloud
- Spring FrameWork
Spring FrameWork
- 是其他项目的根基
- 架构图
- Core Container
- AOP
- 面向切面编程
- Aspects
- AOP思想实现
- Data Access/Data Integration
- Transactions
- 事务
- Web
- web开发
- Test
- 单元测试与集成
核心容器
- IOC
- 程序不主动new对象,由外部提供对象
- 控制反转
- 减少程序耦合
- Spring提供了IOC容器,用来提供对象
- 管理对象的创建和初始化过程
- 被创建或被管理的对象在IOC容器中称为Bean
- DI
- 依赖注入
- 在IOC容器中建立Bean与bean之间的依赖关系的整个过程
- 使用对象可以直接从IOC中获取,并且获取到的bean绑定了所有的依赖关系
bean
- bean基础配置
- Spring默认给我们创建的bean是单例的
- scope
- prototype
- singleton
- scope
- Spring帮我们管理可以复用的对象
- 适合交给容器管理的bean
- 表现层对象
- 业务层对象
- 数据层对象
- 工具对象
- Spring默认给我们创建的bean是单例的
- bean实例化
- 构造方法
- 静态工厂
- 实例工厂
- FactoryBean
- Spring报错
- 从下往上看
- bean的生命周期控制
- 在声明周期前后可以定义初始之前、销毁之后的函数
- 方式一
- init-method
- destory-method
- 方式二
- InitializingBean
- Disposalebean
- 生命周期
- 创建对象 分配内存
- 执行构造方法
- 执行set方法
- 执行bean初始化方法
- 执行业务操作
- 执行bean销毁方法
- 触发bean的销毁
- ConfigurableApplicationContext
- close()
- registerShutdownHook()
- ConfigurableApplicationContext
- 依赖注入
- 方式
- setter注入
- property
- 简单类型
- value
- 引用类型
- ref bean
- 构造器注入
- Constructor-arg
-
- type
- index
- name
- 简单类型
- value
- 引用类型
- ref
- setter注入
- 依赖自动装配
- 用于引用类型的自动装配
- 不能用于简单类型
- 优先级低于setter注入和构造器注入
- 容器根据bean需要的资源自动帮忙配置属性
- autowrie
- byType
- 按类型装配不用起名
- 推荐使用
- byName
- byType
- 集合注入
- 数组
- array
- value>
- value>
- array
- List
- list>
- value>
- list>
- Set
- set>
- value>
- set>
- Map
- map>
- entry key = ‘’ value = ‘’>
- map>
- Property
- props>
- prop key = ‘’></pop>
- props>
- 数组
- 方式
- 案例:数据源对象管理
- 数据库连接池
- 配置
- druid
- c3p0
- 加载properties文件
- 开启context命名空间
- 使用context空间加载properties文件
- context : property-placeholder location=’jdbc.properties’
- 使用属性占位符 读取${}$属性
核心容器总结
- BeanFactory
- IOC容器的顶层接口
- 延迟加载
- ApplicaiotnContext
- 核心接口
- 初始化bean时立即加载
- 提供bean操作的相关接口,通过其他接口扩展其功能
- 初始化类
- ClassPathXmlApplicationContext
- Bean
- id
- name
- class
- scope
- init-method
- destory-method
- autowire
- factory-method
- factory-bean
- lazy-init
- constructor-arg
- property
注解开发
注解开发Bean
- Spring 2.5
- 配置类
- @Configuration
- 数据库配置类导入
- @Import(JdbcConfig.class)
- @Component(“name”)
- 组件
- 相当于 bean标签
- 需要在xml配置扫描
context:component-scan base-package:"com.tianlef"
- 也可以使用类代替xml配置
- @Configruation
- @ComponentScan(“com.tianlef”)
- AnnotationConfigruationContext
- 可以不给名称
- 衍生注解
- @Service
- 业务层bean
- @Repository
- 数据层注解
- @Controller
- @Service
- Bean管理
- @Scope
- prototype
- singleton
- @PostConstruct
- @PreDestroy
- @Scope
- 依赖注入
- 自动装配
- @Autowired
- 按类型装配
- 使用反射里的暴力反射
- 对私有属性进行访问
- 有多个同类型的bean
- 指定加载某一个bean
- @Qualifier(“name”)
- 简单类型自动装配
- @Value(“”)
- @Autowired
- 自动装配
- 管理第三方bean
- @Bean注解 加在方法上 (返回第三方类型)
- 简单类型@Value注入
- 引用类型只需要设置形参,容器会根据类型自动装配对象
MyBatis整合Spring
- Spring JDBC
- Spring整合Mybatis
- 框架的好处在于大量的配置采用默认值
- 主要管理SQLSessionFactory
- 通过SqlSessionFactoryBean
Spring 配置
- Scan函数
- 声明为Spring配置
- 加载Property
AOP
- Aspect Oriented Programming
- 在不惊动原始设计的基础上做增强
- AOP工作模式
- 代理模式
- AOP切入点表达式的语法格式
- 通知类型
- Before
- After
- Round