Spring的基于AspectJ的AOP开发

Spring的基于AspectJ的AOP开发

1、AspectJ简介

  • AspectJ是一个基于Java语言的AOP框架
  • 使用AspectJ需要导入Spring AOP和AspectJ相关jar包
    • spring-aop-4.2.4.RELEASE.jar
    • com.springsource.org.aopalliance-1.0.0.jar
    • spring-aspects-4.2.4.RELEASE.jar
    • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

2、基于AspectJ的注解方式的AOP开发

1、准备工作:

  1. 创建项目

  2. 配置jar包

    <!--引入Spring的基本开发包-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
    
        <!--aop-->
        <dependency>
          <groupId>aopalliance</groupId>
          <artifactId>aopalliance</artifactId>
          <version>1.0</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
    
        <!--aspectj开发的jar包-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.8.9</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
    
        <!--spring整合单元测试的jar包-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
    
  3. 在 applicationContext.xml中开启AspectJ的注解开发

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--==========开启AspectJ的注解开发,自动代理==========-->
    <aop:aspectj-autoproxy/>
</beans>

2、通知的类型介绍

@AspectJ提供不同的通知类型:

  • @Before 前置通知(增强),相当于BeforeAdvice 例:登陆前验证
  • @AfterReturning 后置通知,相当于AfterReturningAdvice 例:删除后显示日至记录
  • @Around 环绕通知,相当于 MethodInterceptor 例:进行事务管理时 (可以阻止目标方法执行)
  • @AfterThrowing 异常抛出通知,相当于ThrowAdvice
  • @After 最终final通知,不管是否异常,该通知都会执行

3、切入点表达式的定义

在通知中通过value属性定义切点

  • 通过execution函数,可以定义切点的方法切入

  • 语法: 访问修饰符 可以不写

    • execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
  • 例如:

    • 匹配所有类 public 方法 execution(public * *(…))

    • 匹配指定包下所有类方法 execution(* com.tao.dao.*(…)) 不包含子包

    • execution(* com.tao.dao…*(…)) 表示包、子孙包下所有类

    • 匹配指定类所有方法 execution(* com.tao.service.UserService.*(…))

    • 匹配实现特定接口所有类的方法

      execution(* com.tao.dao.GenericDao+.*(…))

    • 匹配所有 save 开头的方法 execution(* save*(…))

定义切面类
/**
 * 切面类
 */
@Aspect
public class MyAspectAnno {
    @Before(value = "execution(* com.tao.aspectJ.demo1.ProductDao.save(..))")
    public void before(){
        System.out.println("前置通知=====");
    }
}

4、@Before前置通知

  • 可以在方法中传入JoinPoint对象,用来获得切点信息
@Before(value = "execution(* com.tao.aspectJ.demo1.ProductDao.save(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知=====" + joinPoint);
    }

5、@AfterReturing 后置通知

  • 通过returning属性可以定义方法返回值,作为参数
@AfterReturning(value = "execution(* com.tao.aspectJ.demo1.ProductDao.update(..))",returning = "result")
    public void afterReturing(Object result){
        //Object中的result需要与returning = "result"中的一致
        System.out.println("后置通知=====" + result);
    }

6、@Around 环绕通知

  • around 方法的返回值就是目标代理方法执行返回值

  • 参数为 ProceedingJoinPoint 可以调用拦截目标方法执行,可以阻止目标方法执行

  • 重点∶如果不调用 ProceedingJoinPoint 的proceed方法,那么目标方法就被拦截了

    @Around(value = "execution(* com.tao.aspectJ.demo1.ProductDao.delete(..))")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("环绕前通知=======");
            Object obj = joinPoint.proceed();//执行目标方法
            System.out.println("环绕后通知=======");
            return obj;
        }
    

7、@After Throwing 异常抛出通知

  • 通过设置 throwing 属性,可以设置发生异常对象参数
//出现异常时才会产生通知
@AfterThrowing(value = "execution(* com.tao.aspectJ.demo1.ProductDao.findOne(..))",throwing = "e")
    public void afterThrowing(Throwable e){
        //Throwable 中的 e 要与 throwing = "e"中的 e 一致
        System.out.println("异常抛出通知=========" + e.getMessage());
    }

8、@After 最终通知

  • 无论是否出现异常,最终通知总是会被执行的 ==》 相当于finally
@After(value = "execution(* com.tao.aspectJ.demo1.ProductDao.findAll(..))")
    public void after(){
        System.out.println("最终通知=========");
    }

9、通过@Pointcut为切点命名

  • 在每个通知内定义切点,会造成工作量大,不易维护,对于重复的切点,可以使用 @Pointcut 进行定义
  • 切点方法:private void 无参数方法,方法名为切点名
  • 当通知多个切点时,可以使用 || 进行连接
/**
 * 切面类
 */
@Aspect
public class MyAspectAnno {

    @Before(value = "myPointcut1()")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知=====" + joinPoint);
    }

    @AfterReturning(value = "myPointcut2()",returning = "result")
    public void afterReturing(Object result){
        //Object中的result需要与returning = "result"中的一致
        System.out.println("后置通知=====" + result);
    }

    @Around(value = "myPointcut3()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知=======");

        Object obj = joinPoint.proceed();//执行目标方法

        System.out.println("环绕后通知=======");

        return obj;
    }

    @AfterThrowing(value = "myPointcut4()",throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("异常抛出通知=========" + e.getMessage());
    }

    @After(value = "myPointcut5()")
    public void after(){
        System.out.println("最终通知=========");
    }

    @Pointcut(value = "execution(* com.tao.aspectJ.demo1.ProductDao.save(..))")
    private void myPointcut1(){ }

    @Pointcut(value = "execution(* com.tao.aspectJ.demo1.ProductDao.update(..))")
    private void myPointcut2(){ }

    @Pointcut(value = "execution(* com.tao.aspectJ.demo1.ProductDao.delete(..))")
    private void myPointcut3(){ }

    @Pointcut(value = "execution(* com.tao.aspectJ.demo1.ProductDao.findOne(..))")
    private void myPointcut4(){ }

    @Pointcut(value = "execution(* com.tao.aspectJ.demo1.ProductDao.findAll(..))")
    private void myPointcut5(){ }
}

2、基于AspectJ的XML方式的AOP开发

1、环境搭建

  1. 创建项目
  2. 引入jar包
  3. 创建配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--XML的配置方式完成AOP的开发==========-->
    <!--配置目标类-->
    <bean id="cunsomerDao" class="com.tao.aspectJ.demo2.CustomerDaoImpl"/>   
</beans>

2、使用XML配置切面类

  • 编写切面类

    /**
     * 切面类
     */
    public class MyAspectXml {
    
        //前置通知
        public void before(JoinPoint joinPoint) {
            System.out.println("XML方式的前置通知===========" + joinPoint);
        }
    
        //后置通知
        public void afterReturing(Object result) {
            System.out.println("XML方式的后置通知===========" + result);
        }
    
        //环绕通知
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("环绕前通知===========");
            Object obj = joinPoint.proceed();
            System.out.println("环绕后通知===========");
            return obj;
        }
    
        //异常抛出通知
        public void afterThrowing(Throwable e){
            System.out.println("XML方式的异常抛出通知===========" + e.getMessage());
        }
    
        //最终通知
        public void after(){
            System.out.println("XML方式的最终通知===========");
        }
    }
    
  • 完成切面类的配置

    <!--配置切面类-->
        <bean id="myAspectXml" class="com.tao.aspectJ.demo2.MyAspectXml"/>
    
  • 配置AOP完成增强

    <!--aop的配置-->
        <aop:config>
            <!--配置切入点:哪些类的哪些方法需要增强-->
            <aop:pointcut id="pointcut1" expression="execution(* com.tao.aspectJ.demo2.CustomerDao.save(..))"/>
            <aop:pointcut id="pointcut2" expression="execution(* com.tao.aspectJ.demo2.CustomerDao.update(..))"/>
            <aop:pointcut id="pointcut3" expression="execution(* com.tao.aspectJ.demo2.CustomerDao.delete(..))"/>
            <aop:pointcut id="pointcut4" expression="execution(* com.tao.aspectJ.demo2.CustomerDao.findOne(..))"/>
            <aop:pointcut id="pointcut5" expression="execution(* com.tao.aspectJ.demo2.CustomerDao.findAll(..))"/>
    
            <!--配置AOP的切面-->
            <aop:aspect ref="myAspectXml">
                <!--配置前置通知-->
                <aop:before method="before" pointcut-ref="pointcut1"/>
                <!--配置后置通知-->
                <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
                <!--配置环绕通知-->
                <aop:around method="around" pointcut-ref="pointcut3"/>
                <!--配置异常抛出通知-->
                <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
                <!--配置最终通知-->
                <aop:after method="after" pointcut-ref="pointcut5"/>
            </aop:aspect>
        </aop:config>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值