【AOP】基于@Aspect的AOP配置

基于spring cloud的aop配置

1,启动类MemberAppliaction增加注解

@Import({SwaggerConfiguraion.class, WebMvcAutoConfiguration.class})
@SpringBootApplication
@FFanApplication
@EnableFFanApiDoc
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy=true)  //启动aspect的aop注解
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableDiscoveryClient
@ComponentScan(basePackages = {"cn.wanda.sail.member"})
@MapperScan(basePackages = {"cn.wanda.sail.member.mapper"})
@EnableFeignClients(basePackages = {"cn.wanda.sail.member.client"})
public class MemberApplication {

2,定义切面类

@Aspect
@Component
public class ProcessorAspect {

    private static final Logger log = LoggerFactory.getLogger(ProcessorAspect.class);

3,定义切入方法

    @Around("execution(public * cn.wanda.sail.member.task.support.MemberTask.process(..))")  //环绕增强
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
        if (joinPoint.getArgs() == null || joinPoint.getArgs().length == 0) {
            return joinPoint.proceed();
        }
        TaskContext task = (TaskContext) joinPoint.getArgs()[0];
        log.info("LOG00020: {}  start.....", task.getTaskId());
        long start = System.currentTimeMillis();
        Object result = null;
        try {
            result = joinPoint.proceed();   //执行目标方法
        } catch (Exception exception) {
            handleException(task.getTaskId(), exception);
        }
        log.info("LOG00040: {} end cost : {} ms", task.getTaskId(), (System.currentTimeMillis() - start));

        return result;
    }

 

4,常用的切入方法

   (1),后置返回通知

            @AfterReturning("execution(* com.sxit..*.*(..))")  

   (2),后置异常通知

            @AfterThrowing("execution(* com.sxit..*.*(..))")  

   (3),后置最终通知

            @After("execution(* com.sxit..*.*(..))")  

   (4),环绕通知

            ("execution(* com.sxit..*.*(..))") 

   (5),前置通知

           @Before("execution(* com.sxit..*.*(..))")  

5,定义切入点

  @Pointcut("execution(* com.sxit..*.*(..))")   //针对具体要切入的方法进行说明,如果这块有说明,则切入方法@Before,@Around 都可不用再声明其具体的切入方法,直接声明pointcut声明的方法即可
   public void init(){  
    }  

@Before(value="init()")  
        public void before(){  
            System.out.println("方法执行前执行.....");  
        }  

     

 

 

AOP详解

AOP核心概念

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

 

转载于:https://www.cnblogs.com/lodor/p/7716325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值