`

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();
}
}
分享到:
评论

相关推荐

    spring日志记录格式

    spring日志记录格式

    利用Spring机制记录系统日志

    Spring框架,作为一个广泛应用的Java企业级开发框架,提供了丰富的功能来支持开发者实现系统日志的记录。本篇将详细探讨如何利用Spring机制来实现系统操作日志的统一记录。 首先,让我们了解“AutoLogAdvice.java”...

    spring2.0升级到spring3.0.5的开发包

    Spring框架是Java应用程序开发中的一个核心组件,它提供了一个丰富的IOC(Inversion of Control,控制反转)和AOP(Aspect-Oriented Programming,面向切面编程)功能,使得开发者能够更方便地管理对象和实现模块化...

    spring+hibernate整合实现简单数据库添加记录

    本文将深入探讨如何将Spring与Hibernate整合,以实现对数据库的简单添加记录功能,这对于初学者来说是一次宝贵的学习机会。 首先,我们需要理解Spring的核心概念。Spring提供了一个依赖注入(DI)容器,它负责管理...

    基于Vue和Spring Boot的校园记录管理Web App的设计与实现.pdf

    "基于Vue和Spring Boot的校园记录管理Web App的设计与实现" 本文旨在设计和实现基于Vue和Spring Boot的校园记录管理Web App,以解决当前校园记录管理app的高开发成本和功能不齐全的问题。该系统采用Vue和Spring ...

    spring一些简单的例子

    5. **AOP(面向切面编程)**:Spring的AOP模块允许开发者定义“切面”,这些切面封装了横切关注点,如日志记录、事务管理等。`@Aspect`、`@Before`、`@After`、`@Around`等注解是AOP编程的关键。 6. **Spring MVC**...

    spring一些常用的jar包

    这个压缩包文件"spring一些常用的jar包"显然包含了用于构建和运行Spring应用的基础组件。以下是对这些关键jar包及其功能的详细解释: 1. **springsource**: 这个可能是指Spring的源代码库或者相关的工具,...

    Spring学习笔记(精华全记录)

    ### Spring学习笔记(精华全记录) #### Spring框架概述 Spring框架源自Rod Johnson的个人项目,最初于2002年末发布。Spring并非一开始就作为一个完整的框架出现,而是从一个项目逐步发展而来。随着项目的成熟,...

    spring aop实现接口参数变更前后对比和日志记录

    spring aop实现接口参数变更前后对比和日志记录完整代码,拿到项目代码,只需要做数据库连接的修改即可运行起来使用,代码案例详细,真是可靠,代码原文地址:...

    Spring Boot整合Spring Batch,实现批处理

    对于批处理场景,Spring Boot可以轻松配置数据库连接、日志记录、应用监控等基础设施。 其次,**Spring Batch** 是一个用于处理大量数据的框架,它支持读取、处理和写入大量记录。Spring Batch提供了一套完整的API...

    spring简单注解+log4j记录日志

    综上所述,"spring简单注解+log4j记录日志"这个主题涵盖了Spring框架中的注解使用、Log4j日志系统以及AOP的应用。通过学习这些内容,初学者可以更好地理解和实践Spring框架,同时提高代码的可维护性和调试效率。在...

    SpringBatch+Spring+Mybatis+MySql (spring batch 使用jar)

    Spring Batch可以提供大量的,可重复的数据处理功能,包括日志记录/跟踪,事务管理,作业处理统计工作重新启动、跳过,和资源管理等重要功能。 业务方案: 1、批处理定期提交。 2、并行批处理:并行处理工作。 3、...

    Spring MVC 学习记录总结1

    在这个学习记录总结中,我们将深入理解Spring MVC的核心概念、主要组件以及其工作流程。 1. Spring MVC 概述 Spring MVC 是Spring框架的一部分,它基于Spring IoC(Inversion of Control,控制反转)容器,简化了...

    Spring aop 记录操作日志 Aspect 源码

    在IT行业中,Spring AOP(面向切面编程)是一种强大的工具,它允许我们在代码中实现横切关注点,如日志记录、权限控制等,而无需侵入业务逻辑。本篇将深入探讨如何使用Spring AOP来记录操作日志,并通过自定义Aspect...

    SpringTest_springtest_spring_java_Framework_

    了解Spring框架的基本知识后,我们可以深入探讨如何配置和使用Spring的IoC容器、如何编写面向接口的代码并通过依赖注入实现解耦、如何使用AOP进行横切关注点的处理,如日志记录和事务管理。此外,还可以学习Spring ...

    spring aop 自定义注解保存操作日志到mysql数据库 源码

    4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、...

    spring类库 spring类库

    3. **Spring AOP**:AOP模块实现了切面编程,允许定义切入点和通知,用于在特定方法执行前后插入代码,如事务管理、日志记录等。Spring AOP可以通过代理模式实现,支持JDK动态代理和CGLIB代理。 4. **Spring JDBC和...

    Spring Cloud Gateway 整合 Spring Security 统一登录认证鉴权

    6. **监控和日志**:为了保证系统的健壮性,应设置合适的监控和日志记录,以便在出现问题时能够快速定位。 在压缩包文件`spring_gateway_security_webflux`中,可能包含了示例代码或配置文件,用于演示如何在Spring...

    spring.net结合三层AOP异常日志记录功能

    在本示例中,我们将探讨如何利用Spring.NET在三层架构中实现AOP异常日志记录功能。 1. **三层架构**:在软件开发中,三层架构是一种常见的设计模式,包括表示层、业务逻辑层和数据访问层。表示层负责用户交互,业务...

    spring-cloud项目

    Spring Cloud使得开发人员能够快速地创建一些具有云原生特性的应用,如服务发现、负载均衡、熔断机制等,而无需深入理解底层复杂性。 【描述】"eclipse项目,没有什么内容" 这里提到的是一个基于Eclipse开发的...

Global site tag (gtag.js) - Google Analytics