spring 一些记录

spring初始化代码

DefaultListableBeanFactory.preInstantiateSingletons 启动的beanfactory,创建bean
AbstractAutowireCapableBeanFactory 整个bean创建的过程
populateBean方法 根据property进行autowirebyName或byType注入,对xml配置的属性进行设置
initializeBean方法 初始化bean:下面是回调接口
  BeanNameAware.setBeanName
  BeanClassLoaderAwre.setBeanClassLoader
  BeanFactoryAware.setBeanFactory
  对BeanPostProcessor进行回调beanProcessor.postProcessBeforeInitialization
  InitialzingBean.afterPropertiesSet
  对initMethodName进行调用
  对BeanPostProcessor进行回调applyBeanPostProcessorsAfterInitialization



spring bean lifecycle

Action Description
Initialization begins as bean is instantiated The new bean is instantiated via a constructor, or by calling afactory method, which is considered equivalent. This happensas a result of a getBean() call on the factory, or thefact that another bean that was already being instantiatedhad a dependency on this one, triggering the creation of thisone first.
Dependencies injected Dependencies are injected into the instance, as previouslydiscussed.
setBeanName() called If the bean implements the optional interface, BeanNameAware, then that interface’s setBeanName() methodis called to provide the bean with its primary ID as declared inthe bean definition.
setBeanFactory() called If the bean implements the optional BeanFactoryAware interface, then that interface’s setBeanFactory() methodis called to provide the bean with a reference to the factory itis deployed in. Note that since application contexts are alsobean factories, this method will also be called for beansdeployed in an application context, however passing in thebean factory internally used by the context.
setResourceLoader() called If the bean implements the optional ResourceLoaderAware interface, and it is deployed in an application context,then that interface’s setResourceLoader() method iscalled, with the application context as the ResourceLoader.(To be discussed in the next chapter.)
setApplicationEventPublisher calledIf the bean implements the optionalApplicationEventPublisherAware interface, and it isdeployed in an application context, then that interface’s
setApplicationEventPublisher() method is called,with the application context as the ApplicationEventPublisher. (To be discussed in the next chapter.)
setMessageSource() called If the bean implements the optional MessageSourceAwareinterface, and it is deployed in an application context, thenthat interface’s setMessageSource() method is called,with the application context as the MessageSource. (To bediscussed in the next chapter.)
setApplicationContext() called If the bean implements the optional ApplicationContextAware() interface, and is deployed in an application context,then that interface’s setApplicationContext()method is called to provide the bean with a reference to thecontext.
Bean post-processors get “beforeinitialization”callback with bean Bean post-processors, which will be discussed shortly, arespecial handlers that applications may register with the factory.Post-processors get a pre-initialization callback with thebean, which they may manipulate as needed.
afterPropertiesSet() called If the bean implements the InitializingBean markerinterface, then afterPropertiesSet() from this interfaceis called to allow the bean to initialize itself.
Declared init method called If the bean definition defines an initialization method via theinit-method attribute, then this method is called to allowthe bean to initialize itself.
Bean post-processors get “afterinitialization”callback with the beaninstance as argument Any bean post-processors get a post-initialization callbackwith the bean instance, which they may manipulate asneeded, as an argument.
Bean is used The bean instance is actually used. That is, it’s returned tothe caller of getBean(), used to set a property on the otherbean that triggered its creation as a dependency, and so on.Important note: Only singleton beans are tracked past thispoint, with prototype beans being considered as owned bythe client using the bean. As such, the container will orchestrateonly the subsequent lifecycle events for singletonbeans. Any prototype beans have to be fully managed by theclient past this point, including calling any needed destroymethod.
Bean destruction begins As part of the bean factory or application context shutdownprocess, all cached singleton beans go through a destructionprocess, consisting of the actions following this one. Notethat beans are destroyed in appropriate order related to theirdependency relationship, generally the reverse of theinitialization order.
Bean post-processors get “destroy”callback with bean Any bean post-processors implementing theDestructionAwareBeanPostProcessors interface get acallback to manipulate the singleton bean for destruction.
destroy() called If a singleton bean implements the DisposableBean markerinterface, then that interface’s destroy() method is calledto allow the bean to do any needed resource cleanup.
Declared destroy method called If the bean definition of a singleton bean defines a destroymethod via the destroy-method attribute, then this methodis called to allow the bean to release any resources that needto be released.Initialization and Destruction CallbacksThe initialization



spring的动态代理实现方法

DefaultListableBeanFactory.applyBeanPostProcessor 创建类后回调BeanPostProcessor
AnnotationAwareAspectJAutoProxyCreator.postProcessAfterInitialization 回调
AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors 查找advisors
AnnotationAwareAspectJAutoProxyCreator.BeanFactoryAspectJAdvisorsBuilderAdapter.buildAspectJAdvisors 查找全部初始化bean的Aspect是否存在advice,pointcut
AnnotationAwareAspectJAutoProxyCreator.createProxy 创建proxy(此处应该还有其他Processor)
ProxyFactory.getProxy(ClassLoader) 先通过ProxyFactory创建
DefaultAopProxyFactory.createAopProxy(AdviseSupport) 创建proxy
JdkDynamicAopProxy/CglibProxyFactory.createCglibProxy(config) 选择jdk或cglib



动态代理调用流程

JdkDynamicAopProxy.invoke 调用代理方法
advised.getInterceptorsAndDynamicInterceptionAdvice 获得拦截器或advice链表
new ReflectiveMethodInvocation().proceed 创建调用对象并调用
Cglib2AopProxy.invoke 调用代理方法
advised.getInterceptorsAndDynamicInterceptionAdvice 获得拦截器或advice链表
new CglibMethodInvocation().proceed 创建调用对象并调用



aop相关类

ProxyConfig is the base class for all objects than can create AOP proxies.
AdvisedSupport holds configuration for objects with a single TargetSource and set of interfaces.
ProxyFactoryBean is used to define proxies in XML or other metadata configuration. As its name indicates, it is a FactoryBean, as discussed in Chapter 3 on Spring's IoC container.
ProxyFactory is used to create proxies programmatically, without an IoC container.
AbstractAutoProxyCreator and subclasses handle "autoproxying," discussed later. They inherit basic configuration from ProxyConfig, but do not extend from AdvisedSupport, as TargetSource, interfaces, andother AdvisedSupport configuration parameters are relevant to a single proxy type, rather than generic proxycreation.




spring梳理依赖关系方法//TODO

DispatchServlet属性

Parameter Description Default Value
contextClass Type of WebApplicationContext to use XmlWebApplicationContext
namespace The namespace of the WebApplicationContext to use [name of servlet]-servlet
contextConfigLocation Parameter to override the default value of location(s) to look for WebApplicationContext(s) /WEB-INF/[namespace].xml
publishContext Whether or not to publish to ApplicationContext in the ServletContext (making it available to other Servlets) true

spring mvc 如何实现参数自动映射

解决问题 实现类 思路
方法中String类型的形式参数映射 LocalVariableTableParameterNameDiscoverer 使用asm包的ClassReader
     

spring mvc hander 拦截器

HandlerInterceptor 

动态数据源,配置AbstractRoutingDataSource

public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
protected Object determineCurrentLookupKey() {
        //String key = DataSourceContextHolder.getDataSourceKey();
return DataSourceContextHolder.getDataSourceKey();
}
}

猜你喜欢

转载自jiangyongyuan.iteye.com/blog/592031