Spring框架学习笔记6-AOP编程-AspectJ方式

4 AspectJ框架
4.1AspectJ框架简介
一个基于java的aop框架
在Spring2.0后增加了对AspectJ框架的支持。在Spring框架中建议使用AspectJ框架开发AOP.
4.1.1AspectJ框架中的通知类型
前面四个与spring aop框架相同。
前置通知before
后置通知afterReturning
环绕通知around
异常通知afterThrowing
最终通知after :无论程序发生任何事情,都将执行
4.1.2Spring整合AspectJ框架所依赖的jar包
4.1.2.1AspectJ框架jar包
aspectjweaver-1.9.5.jar
4.1.2.2Spring框架jar包
beans
context
core
experssion
aop
aspects 这个是spring框架整合aspectj的jar包,spring框架本身不包含这个aspectj,整合第三方框架aspectj的jar包
commons-logging
4.1.3Aspect框架配置AOP的方式
两种:xml;注解
xml:
AspectJ配置(第三方框架自己的方式)
Spring的Schema_based方式
4.2AspectJ框架的使用
4.2.1AspectJ配置方式
意思是用AspectJ框架的配置方式来配置切面,在使用AspectJ配置切面时,切面不需要实现一些特定的接口。
4.2.1.1创建切面

package com.bjsxt.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
   
    /**
     * 前置通知
     * @param joinPoint 对目标对象的封装
     */
    public void myBefore(JoinPoint joinPoint) {
   
        //获取目标对象
        //joinPoint.getTarget();
        //获取目标对象目标方法名
        //joinPoint.getSignature().getName();
        //获取目标方法的参数列表
        //joinPoint.getArgs();
        //获取代理对象
        //joinPoint.getThis();
        System.out.println("Before..."+joinPoint.getSignature().getName());
    }

    /**
     * 后置通知
     * @param joinPoint 对目标对象的封装
     */
    public void myAfterReturning(JoinPoint joinPoint) {
   
        System.out.println("After..."+joinPoint.getSignature().getName());
    }

    /**
     * 环绕通知
     * @param proceedingJoinPoint 继承JoinPoint 对他做增强,增加调用目标方法的语句
     * @return
     * @throws Throwable
     */
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
   
        System.out.println("Around Before..."+proceedingJoinPoint.getSignature().getName());
        //调用目标方法
        Object o =proceedingJoinPoint.proceed();
        System.out.println("Around After..."+proceedingJoinPoint.getSignature().getName());
        return o;
    }

    /**
     * 异常通知
     * @param e 目标方法执行抛异常后执行这个方法
     */
    public void myAfterThrowing(Exception e){
   
        System.out.println("Exception "+e);
    }

    /**
     * 最终通知
     * 不要参数
     */
    public void myAfter(){
   
        System.out.println("最终通知");
    }
}

4.2.1.2Execution表达式
是AspectJ框架的一种表达方式,用于配置切点(目标方法).
基本语法格式为:
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
其中<修饰符模式>与<异常模式>为可选
execution(publiccom.bjsxt.service….*(…))
说明:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.2.1.3使用AspectJ方式配置切面(xml)
4.2.1.3.1创建目标对象

package com.bjsxt.service;

public interface UsersService {
   
    void addUsers(String username);
}
package com.bjsxt.service.impl;

import com.bjsxt.service.UsersService;

public class UsersServiceImpl implements UsersService {
   
    @Override
    public void addUsers(String username){
   
        System.out.println("addUsers "+username);
    }

}

2.1.3.2开启aop命名空间
在这里插入图片描述

<?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">

4.2.1.3.3配置切面

<?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">
    <!--配置目标对象-->
    <bean id="userService" class="com.bjsxt.service.impl.UsersServiceImpl"></bean>
    <!--配置切面对象-->
    <bean id="myAspect" class="com.bjsxt.aop.MyAspect"></bean>
    <!--配置切面-->
    <aop:config>
        <aop:aspect ref="myAspect">
            <!--配置切点-->
            <aop:pointcut id="myPointcut" expression="execution(* com.bjsxt.service.*.*(..))"></aop:pointcut>
            <!--前置通知-->
            <aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before>
            <!--后置通知-->
            <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut"></aop:after-returning>
            <!--环绕通知-->
            <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>
            <!--异常通知-->
            <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
            <!--最终通知-->
            <aop:after method="myAfter" pointcut-ref="myPointcut"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

4.2.1.3.4创建测试类

package com.bjsxt.test
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值