Spring 简介
Spring 是为Java应用程序提供基础服务的一套框架,重点包括IOC和AOP思想,目的在于简化企业应用程序的开发,让开发人员着重关注业务需求。
Spring 主要模块
- spring-core
- spring-context
- spring-bean
- spring-aop
- spring-web
- spring-webmvc
- spring-tx
- spring-jdbc
Spring IOC
IOC就是控制反转,原先由程序自己通过new创建对象,有了IOC,对象的创建及依赖管理控制权就交给了IOC容器
Spring AOP
AOP就是面向切面编程,抽取出公共模块逻辑,常用于权限管理、日志、事务处理
BeanFactory 和 ApplicationContext的区别
BeanFactory 懒加载bean,是Spring最底层的接口容器
ApplicationContext 是BeanFactory的子接口,一次性加载所有bean
Spring bean的作用域
- singleton 默认,容器中只有一个bean实例
- prototype 为每个请求创建一个实例
- request 为每个Http请求,创建一个实例,只在Http请求有效期内
- session 为每个http session 创建一个实例,只在session有效期内
- global-session 全局作用域 仅在基于protlet的web应用有意义
当使用支持WebApplicationContext时,最后三个才有用
Spring bean 生命周期
- 实例化(new操作)
- 属性赋值
- 实现Aware接口,让bean具备Spring一些对象,比如AppliactionContext
- 若bean 实现了InitializingBean接口,则调用对应初始化属性后的方法(afterPropertiesSet)
- 若bean 实现了DisposableBean,则bean在销毁的时候,会调用方法(destroy)
Spring 配置方式
- 基于XML配置
- 基于Java注解配置
- 基于Java类配置
Spring bean注入方式
- 构造方法注入
- 属性set方法注入
Spring 支持事务类型
- 声明式事务
- 编程式事务
Spring Context 初始化过程
类:org.springframework.context.support.AbstractApplicationContext
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备上下文,初始化environment,propertySource
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 获取新的beanFactory,并扫描所有的beanDefinition
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 初始化beanFactory属性及bean,比如自动装配处理器
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 添加BeanPostProcessor处理器,在初始化BeanFactory后处理
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 调用所有BeanPostProcessor
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 当bean被创建后,会依次调用所有bean的BeanPostProcessors
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 若存在MessageSource bean 则初始化
initMessageSource();
// Initialize event multicaster for this context.
// 初始化应用事件多点传送
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 刷新操作,由子类实现
onRefresh();
// Check for listener beans and register them.
// 注册listeners到ApplicationEventMulticaster
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
// 初始化非lazy-init的单例对象
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 完成刷新事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
Spring 使用注意事项
- 合理使用代码目录结构
- 定义包名,如:com.web.dao
- 启动类存放在顶级源码目录中
@Controller和@Service要简洁和专注
DAO层独立于核心业务代码之外
配置外部化
- 使用配置服务器 如:eurka config
- 使用Git库
使用日志框架,按照统一的日志输出格式
强制要求写测试单例 如单元测试、集成测试