美文网首页
[原创]【注解驱动开发3】- AOP

[原创]【注解驱动开发3】- AOP

作者: 垃圾简书_吃枣药丸 | 来源:发表于2019-04-17 11:30 被阅读0次

一. 使用

  • 目标方法
interface DivService {
    default int div(int a, int b) {
        return a / b;
    }
}
/**
 * 业务逻辑,目标方法
 */
@Slf4j
@Service
class DivServiceImpl implements DivService {
    @Override
    public int div(int a, int b) {
        log.info("div目标方法被执行...");
        return a / b;
    }
}
  • 定义切面
/**
 * LogAop
 */
@Component
@Slf4j
@Aspect
class LogAspect {

    @Pointcut("execution(public int com.futao.springmvcdemo.test.aspect.DivService.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint point) {
        log.info("【{}】方法执行前@Before,参数列表:【{}】", point.getSignature().getName(), point.getArgs());
    }

    /**
     * 无论目标方法是否成功,都会执行该方法
     *
     * @param point
     */
    @After(value = "pointCut()")
    public void after(JoinPoint point) {
        log.info("【{}】方法执行后@After", point.getSignature().getName());
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void afterReturning(JoinPoint point, Object result) {
        log.info("【{}】方法执行后正常返回@AfterReturning,返回值:【{}】", point.getSignature().getName(), result);
    }

    @AfterThrowing(value = "pointCut()", throwing = "e")
    public void afterThrowing(JoinPoint point, Exception e) {
        log.error("【{}】方法执行后发生异常,异常信息为【{}】", point.getSignature().getName(), e.getMessage());
    }
}
  • 定义配置类,开启AspectJ动态代理
@ComponentScan("com.futao.springmvcdemo.test.aspect")
@Configuration
@EnableAspectJAutoProxy  //开启AspectJ动态代理
class AspectJSpringConfig {
}

//或者
<aop:aspectj-autoproxy/>
  • 测试
/**
 * @author futao
 * Created on 2019-04-17.
 */
public class AspectJTest {
    @Test
    public void test1() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AspectJSpringConfig.class);
        DivService divService = applicationContext.getBean(DivService.class);
        divService.div(1, 1);
        System.out.println(StringUtils.repeat("==", 50));
        divService.div(1, 0);
    }
}
  • 结果
image.png

# 开发步骤以及注意事项

  1. 开发步骤
    • 定义目标方法
    • 定义切入点,切面等方法
    • 将目标类与切面类都加入Spring容器中
    • 将切面类加上@Aspect注解,表明这是一个切面类
    • 开启Spring的注解式切面@ EnableAspectJAutoProxy
  2. 注意事项
    • 目标类和切面类都需要加入到Spring容器中,交给Spring来管理。
    • 通过new 目标类的方式生成的对象不会被切面拦截。

相关文章

  • [原创]【注解驱动开发3】- AOP

    一. 使用 目标方法 定义切面 定义配置类,开启AspectJ动态代理 测试 结果 # 开发步骤以及注意事项 开发...

  • AOP源码分析之@EnableAspectJAutoProxy

    上节我们在Spring注解驱动开发AOP功能的篇幅中简单的通过案例来回顾了AOP的注解驱动开发的基本过程,同时也提...

  • Spring 注解版

    文末有彩蛋 @Scope bean的生命周期 @Value 自动装配 @Profile AOP Spring注解驱动开发

  • Spring注解驱动开发声明式事务功能

    前面我们学习了Aop的注解驱动开发及相关源码的解读过程,从本节开始我们以同样的套路来学习声明式事务的注解开发及源码...

  • Spring注解驱动开发之AOP

    前言:现今SpringBoot、SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了S...

  • Spring注解驱动开发AOP功能

    关于AOP的注解版开发可能大家都知道,本篇通过一个简单的demo来做入门的引导,其目的主要是分析SpringAOP...

  • Spring Aop支持

    Spring提供了四种Aop支持: 基于代理的经典Spring Aop 纯Pojo切面 @AspectJ注解驱动的...

  • 基于AspectJ的注解AOP开发

    基于AspectJ的注解AOP开发 AspectJ开发准备 导入相关jar包 引入aop约束 @AspectJ通知...

  • 06 AOP

    Spring 提供了四种AOP支撑:基于代理的经典spring aop;纯POJO切面@AspectJ注解驱动的切...

  • 【Spring】AOP注解开发及JDBC整合

    AOP注解开发光速入门 步骤一:引入相关的jar及配置文件 步骤二:编写目标类 步骤三:开启aop注解自动代理 常...

网友评论

      本文标题:[原创]【注解驱动开发3】- AOP

      本文链接:https://www.haomeiwen.com/subject/nrvgwqtx.html