spring加载和使用分析
ClassPathXmlApplicationContext cxa = new ClassPathXmlApplicationContext("test.xml");
spring上下文注册过程:
org.springframework.context.support.ClassPathXmlApplicationContext.ClassPathXmlApplicationContext(String configLocation)
org.springframework.context.support.ClassPathXmlApplicationContext.ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
org.springframework.context.support.AbstractApplicationContext.refresh()
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons()
1.创建bean
DefaultListableBeanFactory(getBean)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(String name)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(String beanName, RootBeanDefinition mbd, Object[] args)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(String beanName, RootBeanDefinition mbd, Object[] args)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args)
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner)
org.springframework.beans.BeanUtils.instantiateClass(Constructor<T> ctor, Object... args)
org.springframework.util.ReflectionUtils.makeAccessible(Constructor<?> ctor)
java.lang.reflect.Constructor.newInstance(Object... initargs)
2.给bean设置属性
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs)
org.springframework.beans.BeanWrapperImpl(setPropertyValues)
org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(PropertyValues pvs)
org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv)
org.springframework.beans.BeanWrapperImpl.BeanPropertyHandler.setValue(Object object, Object valueToApply)
java.lang.reflect.Method.invoke(Object obj, Object... args)
3.注册已经创建好的bean
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerDisposableBean(String beanName, DisposableBean bean)
使用,获取bean
getBean by name(id)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(String name)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(String name, Class<T> requiredType, Object[] args, boolean typeCheckOnly)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(String beanName)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(String beanName, boolean allowEarlyReference)
getBean by class
org.springframework.context.support.AbstractApplicationContext.getBean(Class<T> requiredType)
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(Class<T> requiredType)
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(Class<T> requiredType, Object... args)
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit)
循环所有bean定义数组元素,找到class匹配的bean名称,放入缓存(这里比较慢,还不如在解析bean时候就放好,省得调用时候查找bean名称);所以建议getBean使用名称而不是class type
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(String name, Class<T> requiredType, Object... args)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(String name, Class<T> requiredType, Object[] args, boolean typeCheckOnly)
spring事务注解
org.springframework.transaction.annotation.SpringTransactionAnnotationParser.parseTransactionAnnotation(AnnotationAttributes attributes)
rollbackFor noRollbackFor
rbta.getRollbackRules().addAll(rollBackRules); org.springframework.transaction.interceptor.RuleBasedTransactionAttribute.getRollbackRules()
org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionInfo txInfo, Throwable ex)
if (txInfo.transactionAttribute.rollbackOn(ex)) 调用的是org.springframework.transaction.interceptor.RuleBasedTransactionAttribute.rollbackOn(Throwable ex)
如果没有配置规则,调用super.rollbackOn(ex); return (ex instanceof RuntimeException || ex instanceof Error);运行时异常或error
txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
入口是
org.springframework.transaction.interceptor.TransactionProxyFactoryBean.setTransactionManager(PlatformTransactionManager transactionManager)
private final TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(MethodInvocation invocation) throws Throwable
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(Method method, Class<?> targetClass, InvocationCallback invocation) throws Throwable
completeTransactionAfterThrowing(txInfo, ex);