SpringAOP的两种实现方法

电脑杂谈  发布时间:2020-03-27 03:09:43  来源:网络整理

spring aop注解方式_spring aop 几种方式_spring aop 几种方式

1. SpringAOP,面向方面的编程. 总结在实际应用中非常普遍,通常用于日志和异常存储. 也可以为相应的业务进行处理.

AOP核心概念

1. 横切关注点

被拦截的方法有哪些,拦截后如何处理,这些问题称为跨领域问题

2. 方面

类是对象特征的抽象,而方面是跨领域关注点的抽象

3. 连接点

拦截点. 由于Spring仅支持方法类型的连接点,因此Spring中的连接点是指被拦截的方法. 实际上,连接点也可以是字段或构造函数.

4,切入点(切入点)

spring aop注解方式_spring aop 几种方式_spring aop 几种方式

截取连接点的定义

5,建议(建议)

所谓的通知是指在拦截连接点之后要执行的代码. 通知分为五种类型: 前置,后置,异常,最终和环绕通知.

6. 受众

代理商观众

7. 编织

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

8. 简介

在不修改代码的情况下,您可以在运行时动态地向类添加一些方法或字段

spring aop注解方式_spring aop 几种方式_spring aop 几种方式

2. 有两种方法可以配置方面: xml配置(推荐),注释.

3. 第一种方法: 注释

1)在相应的程序包中构建自己的类

2)在相关的类中添加相关的注释,目的是将此类视为一个方面

3)执行相关的节处理. 这里有两种方法,一种是直接方法,另一种是注释方法.

这是第一种方式,它直接与相关类中的方法相对应地进行处理

package com.troy.ai.web.Interceptors;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AOPTEST {
    @Pointcut("execution(* com.troy.ai.web.schedule.ScheduleController.*(..))")
    public void test(){}
    
    @Before("test()") //也可以@Before("execution(* com.troy.ai.web.schedule.ScheduleController.*(..))")
    public void before(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"before");
    }
    
    @After("test()")
    public void after(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"after");
    }
}

第二个: 通过注释实现

spring aop注解方式_spring aop 几种方式_spring aop 几种方式

a. 自己写评论:

import java.lang.annotation.*;
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
    String  description()  default "";
}

b,然后通过注释配置方法实现方面

@Aspect
@Component
public class LogInterceptor {
    private long  startTime;
    private JSONObject json = new JSONObject();
    @Pointcut("@annotation(com.troy.ai.web.Interceptors.SystemLog)")  
    public void  logAspect(){}
    @Before("logAspect()")
    public void before(JoinPoint JoinPoint){}
    @AfterThrowing("logAspect()")  
    public void  afterThrowing(){}
    @After("logAspect()")
    public void  after(JoinPoint JoinPoint){}

这仅需要将此注释添加到要刻面的类的方法中

这样,您可以通过自己的逻辑来实现相关业务.

3)说明: JoinPoint是相应方面提供的接口. 您可以通过JoinPoint查找特定信息.

spring aop 几种方式_spring aop 几种方式_spring aop注解方式

4. xml的配置方法实际上是相似的.

1)仍然编写相应的方面类(无需发表任何评论)

import org.aspectj.lang.JoinPoint;
public class AOPTEST {
    public void before(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"before");
    }
    
    public void after(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"after");
    }
}

2)xml配置

<bean id="AOPTEST" class="com.troy.ai.web.Interceptors.AOPTEST"/>
    <aop:config>
        <aop:aspect id="aspect" ref="AOPTEST">
            <aop:pointcut expression="execution(* com.troy.ai.web.schedule.ScheduleController.*(..))" id="pointcut"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

3)此方法也可以使用注释来实现.

5. 差异

1)共同点: 实施过程中的效果相同,两种方法在参考过程中都很容易理解

2)不同点:

a. 注释的方式便于代码编写,并且易于处理新添加的方法,但是缺点是注释容易分散并且难以管理.

b,xml是集中管理. 无论业务逻辑关系如何变化,所有内容都统一配置以便于管理. 相对而言,使用起来不太舒服spring aop 几种方式spring aop 几种方式,新方法或新方法上会有多种配置

版权声明: 本文的内容由Internet用户自行提供. 版权归作者所有. 社区不拥有所有权,也不承担任何法律责任. 如果您发现该社区存在可疑的窃行为,请发送电子邮件至yqgroup@service.aliyun.com进行举报并提供相关证据. 验证后,社区将立即删除涉嫌侵权的内容.


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-153685-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 谭舜
        谭舜

        在伊拉克却说你没有和交朋友得自由

      • 王玉聪
        王玉聪

        就是为了使别人活得更美好

      • 邓慧
        邓慧

        要那么多航母去捕鱼

      热点图片
      拼命载入中...