aop

Spring Aop

笑着哭i 提交于 2020-03-06 10:18:16
一,AOP 面向切面编程 二,Spring中的AOP 1,自带AOP 2,AspectJ 三,Spring AOP原理 1,jdk动态代理 2,cglib动态代理 3,责任链模式 四,源码相关 1,spring创建对象的三个步骤 ① 实例化 ② 前置处理:前置处理器 ③ 初始化 ④ 后置处理 :后置处理器 spring的aop生成代理对象发生在这里 2,Aop相关接口 InstantiationAwareBeanPostProcessor AbstractAutoProxyCreator.wrapIfNecessary 来源: CSDN 作者: epitomizelu 链接: https://blog.csdn.net/epitomizelu/article/details/104687966

Spring aop not triggered on one method but triggered on the other method

£可爱£侵袭症+ 提交于 2020-03-06 04:43:06
问题 I use AOP to separate monitor from the bussiness logic. But when I use junit to test my aop code I found that AOP are not triggered when method B is executed but are triggered when method A is executed. And method B calls method A. my pseudocode likes below : @Aspect public class TimeMonitor { @Pointcut("execution( * MainClass.A(..))") public void pointA(); @Around("pointA()") Object monitorA(ProceedingJoinPoint jp ){ try{ jp.proceed(); }catch(Exception e){ logger.error("failed to execute A

Spring源码分析笔记--AOP

冷暖自知 提交于 2020-03-05 17:19:02
核心类&方法 BeanDefinition Bean的定义信息,封装bean的基本信息,从中可以获取类名、是否是单例、是否被注入到其他bean中、是否懒加载、bean依赖的bean的名称等。 Aware 继承Aware的bean可以感知到他在容器中的一些属性,如获取bean在容器中的ID,甚至获取到容器等。 BeanPostProcessor Bean的后置处理器,可在bean的创建、初始化等阶段的前后对bean进行增强处理等。 BeanFactory 以 Factory 结尾,表示它是一个工厂类 ( 接口 ) ,用于管理 Bean 的一个工厂。在 Spring 中, BeanFactory 是 IOC 容器的核心接口,它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。 是访问容器的顶层接口,他的实现类中会有一个 MAP ,用于存储 bean 的 BeanDefinition 。 ( 与 FactoryBean 区别:实现 FactoryBean 接口也是在容器中注入 bean 的方式之一,但它不是一个普通的 bean ,而是生成指定 bean 的一个工厂 ) AbstractAutowireCapableBeanFactory :: populateBean ( .. ) DefaultListableBeanFactory 继承了

spring aop

那年仲夏 提交于 2020-03-05 16:10:22
一,spring实现aop 1,基于注解 <aop:aspectj-autoproxy /> xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" //启动对@AspectJ注解的支持 <aop:aspectj-autoproxy /> 2,基于xml配置 <aop:config>... 二,spring与jdbc整合 //专门用来对@Transactional解析,注解本身是不起作用的 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> 首先引入一些要用的包 spring框架需要: commons-logging.jar spring.jar 注解需要: aspectjrt.jar aspectjweaver.jar cglib-nodep-2.1_3.jar common-annotations.jar jdbc数据库,连接池需要: commons-dbcp-1.2.1.jar

Spring(六)AOP切入方式

纵饮孤独 提交于 2020-03-05 16:09:50
一、接口切入方式 实现类 package com.pb.entity; /** * 实体类 */ public class Hello { private String name; private String password; public void show(){ System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 1.1、前置增强 package com.pb.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /** * 前置增强的Bean * @author Administrator

aop错误集锦-error at ::0 formal unbound in pointcut

孤街醉人 提交于 2020-03-05 13:55:24
出錯原因:aop方法参数写漏了 ---aop的afterThrow 漏掉了一个参数ex绑定 public void afterThrow(JoinPoint joinPoint,Exception ex) 原:<aop:after-throwing method="afterThrow" pointcut-ref="xmlPointCut" /> 后:<aop:after-throwing method="afterThrow" pointcut-ref="xmlPointCut" throwing="ex"/> 来源: CSDN 作者: Annie_ya 链接: https://blog.csdn.net/Annie_ya/article/details/104669855

Spring面试笔记

时光总嘲笑我的痴心妄想 提交于 2020-03-05 12:04:54
1. Spring工作机制及为什么要用? Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的。Spring既是一个AOP框架,也是一IOC容器。 SpringFramework的组成:Spring AOP,Spring DAO,Spring ORM,Spring Web,Spring Context, Spring Web MVC。 Spring的核心就是IOC和AOP,所以Spring的工作机制简单的理解也就是IOC和AOP的工作机制。 借助于Spring AOP,Spring IoC能够很方便的使用到非常健壮、灵活的企业级服务,通过使用IoC能够降低组件之间的耦合度,最终,能够提高类的重用性,利于测试,而且更利于整个产品或系统集成和配置。 2. 说说AOP和IOC的概念以及在spring中是如何应用的? AOP,Aspect Oriented Program,面向(方面)切面的编程; IOC,Invert Of Control,控制反转。 简单说一下,IOC就是其实就是依赖注入,即用接口编程,在程序中不出现new关键字,而是用接口来命名引用,然后通过某种方式把接口的某个实现类的实例注入到引用里,从而实现接口与具体实现类的松耦合。 由容器控制程序之间的关系(通过XML配置),而非传统实现中的由程序代码直接操控,(在一个Class对象中引用另一个Class对象时

Spring AOP and AspectJ Load-Time Weaving: Around advice will be invoked twice for private methods

此生再无相见时 提交于 2020-03-05 07:53:46
问题 I will use Spring AOP and AspectJ Load-Time Weaving to measure execution time of specific private/protected/public methods in my code. To do this I wrote following annotation with one I will annotate the methods which execution time should be measured: package at.scan.spring.aop.measuring; import org.aspectj.lang.ProceedingJoinPoint; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;

AOP面试知识整理,^_^-包括spring Aop

妖精的绣舞 提交于 2020-03-04 23:50:07
讲到java企业级开发框架,就不可避免的讲到 IOC,AOP,MCV   今天面试时被问到AOP,讲的很乱,这里整理笔记,包括AOP,spring-AOP的部分知识,错误的地方请小伙伴指出来. 谈谈你对AOP的理解: AOP概念(Aspect-Oriented Programming): 即面向切面编程,与OOP(Object - Oriented Programming,面向对象编程)相辅相成,AOP的基本单元为Aspect(切面),Struts2 的拦截器设计就是基于AOP的思想。 AOP原理: 大型系统中的 通用的服务型的代码会穿插在各个业务类 ,方法中,随着系统规模的增大,会造成 大量的代码重复 ,,且与核心代码没有太多的关系。 系统中的业务可分为 核心关注点和横切关注点 , 核心关注点是业务处理的主要流程,横切关注点是与核心业务无关的通用业务。如日志权限 等,各个横切点离散的穿插与核心业务中。导致系统中的每一个模块代码都与这些业务具有很强的依赖性,当需要添加横切功能时,需要大幅修改已有的代码。 AOP即解决这个问题, 使用AOP框架,能够将这些影响多个类的通用性服务抽取出来,(即切面) ,并通过 配置的方式明确在那些位置插入这些服务, 系统运行后,AOP框架在指定的时机自动运行这些服务,从而达到核心业务逻辑和服务性逻辑分离的目的,减少了重复代码的

spring——AOP原理及源码(二)

跟風遠走 提交于 2020-03-03 22:47:16
回顾: 在 上一篇 中,我们提到@EnableAspectJAutoProxy注解给容器中加入了一个关键组件 internalAutoProxyCreator的BeanDefinition,实际类型为 AnnotationAwareAspectJAutoProxyCreator的BeanDenation 并且发现这是一个后置处理器,也是一个XXXAware接口的实现类。以及探究了它的继承关系如下。 接下来我们就从后置处理器和BeanFactoryAware的角度来看看AnnotationAwareAspectJAutoProxyCreator的BeanDefinition创建完成后都做了什么。 一、设置调试断点 我们分别进入四个有关类,在类中与后置处理器和BeanFactoryAware有关的方法上打上断点。最终效果如下: AbstractAutoProxyCreator.setBeanFactoryAbstractAutoProxyCreator有后置处理器逻辑{  postProcessBeforeInstantiation()  postProcessAfterInitialization() } AbstractAdvisorAutoProxyCreator.initBeanFactoryAbstractAdvisorAutoProxyCreator