【Spring】- 使用注解配置方式实现AOP

前言

    有了前面Spring aop基于xml配置的基础,注解实现AOP就很好理解了。

正文

    1、导入aop 命名空间
    2、准备目标对象
这两步与上篇博客的内容一样,这里不再赘述:SpringAOP的xml配置
    3、准备通知对象

package cn.itcast.d_springaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/*通知类*/
@Aspect
 /*表示该类是一个通知类*/
public class MyAdvice {

    //前置通知   目标方法运行之前调用
    //后置通知 (如果出现异常不会调用) 目标方法运行之后调用
    //环绕通知   目标方法之前和之后都调用
    //异常拦截通知   如果出现异常,就会调用
    //后置通知(无论是否出现异常,都会调用) 在目标方法运行之后调用
    //---------------------------------------

    @Pointcut("execution(* cn.itcast.service.UserServiceImpl.*(..))")
    public void pc(){}
    //前置通知
    //指定该方法是前置通知,并指定切入点
    @Before("MyAdvice.pc()")
    public void before(){
        System.out.println("这是前置通知");
    }


    //后置通知
    @AfterReturning("MyAdvice.pc()")
        public void afterReturning(){
            System.out.println("这是后置通知(如果出现异常不会调用 )");
        }
        //环绕通知
    @Around("MyAdvice.pc()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable{
            System.out.println("这是环绕通知(之前的部分 )");
            Object proceed = pjp.proceed();  //调用目标方法
            System.out.println("这是环绕通知(之后的部分)");
            return proceed;
        }

        //异常通知  可以将切入表达式抽离到一个方法中,例如pc(),这个异常通知还是用重复的切入表达式
    @AfterThrowing("execution(* cn.itcast.service.UserServiceImpl.*(..))")
        public void afterException(){
            System.out.println("出事啦!出现异常了!!");
        }


        //后置通知  重复的切入点表达式
    @After("execution(* cn.itcast.service.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("这是后置通知(如果出现异常也会调用 )");
        }


}

    4、开启注解完成织入(applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">

<!-- <context:component-scan base-package="cn.itcast.*"></context:component-scan> -->

<!--
  1、 准备工作:导入aop(约束)命名空间
  2、 配置目标对象
  3、 配置通知对象
  4、配置将通知织入目标对象 
  -->
    <!-- 目标对象 -->
   <bean name = "userService" class = "cn.itcast.service.UserServiceImpl"></bean>
    <!-- 通知对象 -->
   <bean name = "myAdvice" class = "cn.itcast.d_springaop.MyAdvice"></bean>
    <!-- 开启使用注解完成织入,开启此注解后在通知类中写注解就可以完成对目标对象的织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

    5、测试及结果

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/d_springaop2_anno/applicationContext.xml")
public class demo {

    @Resource(name="userService")
    private UserService us;
    @Test
    public void fun1(){
        us.save();

    }
}

这里写图片描述

总结

    注解方式实现AOP 先介绍到这里,感谢您的阅读!

猜你喜欢

转载自blog.csdn.net/zt15732625878/article/details/81335434