1. spring 启动refresh():
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
//主要是创建beanFactory,同时加载配置⽂件.xml中的beanDefinition
//通过String[] configLocations = getConfigLocations()获取资源路径,然后加载beanDefinition
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 给beanFactory注册⼀些标准组件,如ClassLoader,BeanPostProcess
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
//提供给⼦类实现⼀些postProcess的注册,如AbstractRefreshableWebApplicationContext注册⼀些Servlet相关的
//postProcess,真对web进⾏⽣命周期管理的Scope,通过registerResolvableDependency()⽅法注册指定ServletRequest,HttpSession,WebRequest对象的⼯⼚⽅法
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
//调⽤所有BeanFactoryProcessor的postProcessBeanFactory()⽅法
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
//注册BeanPostProcessor,BeanPostProcessor作⽤是⽤于拦截Bean的创建
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
//初始化消息Bean
initMessageSource();
// Initialize event multicaster for this context.
//初始化上下⽂的事件多播组件,ApplicationEvent触发时由multicaster通知给ApplicationListener
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
//ApplicationContext初始化⼀些特殊的bean
onRefresh();
// Check for listener beans and register them.
//注册事件监听器,事件监听Bean统⼀注册到multicaster⾥头,ApplicationEvent事件触发后会由multicaster⼴播
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
//⾮延迟加载的单例Bean实例化
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();
}
}
}
2. 声明式事务源码
a. @EnableTransactionManagement:利⽤TransactionManagementConfigurationSelector给容器中导⼊两个组件:
- i. AutoProxyRegistrar;
- ii. ProxyTransactionManagementConfiguration。
b. AutoProxyRegistrar:给容器注⼊InfrastructureAdvisorAutoProxyCreator组件。利⽤后置处理器在对象创建以后,包装对象,返回⼀个代理对象(增强器),代理对象执⾏⽬标⽅法时执⾏拦截器链。
c. ProxyTransactionManagementConfiguration:给容器注册增强器。
- i. 设置事务注解信息:AnnotationTransactionAttributeSource解析事务注解;
- ii. 设置事务拦截器:TransactionInterceptor(是⼀个MethodInterceptor),保存了事务属性信息和事务管理器。 在⽬标⽅法执⾏的时候,执⾏拦截器链
protected Object invokeWithinTransaction(Method method, Class targetClass, final InvocationCallback invocation
throws Throwable {
// If the transaction attribute is null, the method is non-transactional.
final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute
final PlatformTransactionManager tm = determineTransactionManager(txAttr);
final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
// Standard transaction demarcation with getTransaction and commit/rollback calls.
TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification
Object retVal = null;
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
retVal = invocation.proceedWithInvocation();
} catch (Throwable ex) {
// target invocation exception
completeTransactionAfterThrowing(txInfo, ex);
throw ex;
} finally {
cleanupTransactionInfo(txInfo);
}
commitTransactionAfterReturning(txInfo);
return retVal;
}
}
3. newrelic源码:
javaagent、asm操控字节码,类加载时在指定类前后增加拦截器。
openresty + lua开发⾼性能web服务。转发⾄kafka