• Spring aop


    Spring AOP(Aspect-Oriented Programming

    - 注解配置代理

    AOP(面向切面编程)

    注解 的xml配置: <aop:aspectj-autoproxy/>

    开启自动扫描: <context:component-scan base-package="com.yuing.spring1.aopaspect"/>

    代理类

    @Aspect
    @Component
    @Order(1)   //多个切面进行工作时加@Order注解 ,
    public class Logging {
        //@Pointcut 相当于一个模板`* com.yuing.spring1.aopaspect` 
            @Pointcut("execution(* com.yuing.spring1.aopaspect.*.*(..))")
            public void pointcut(){}
    
        //前置通知
            @Before("execution(public void com.yuing.spring1.aopaspect.AddServer.*(int,int))")
            public void beform(JoinPoint joinPoint){
                String name = joinPoint.getSignature().getName();
                Object[] args = joinPoint.getArgs();
                System.out.println("方法的名称:"+name+"   方法的参数:"+ Arrays.toString(args));
            }
            //后置通知
            @After("execution(public void com.yuing.spring1.aopaspect.AddServer.*(..))") //(..)相当于Object...
            public void after(){
                System.out.println("执行方法之后");
            }
            //返回通知  在注解中定义方法的返回值信息
            @AfterReturning(value = "execution(* com.yuing.spring1.aopaspect.*.*(int,int))",returning = "result")
            public void afterReturning(JoinPoint joinPoint,Object result){
                Signature signature = joinPoint.getSignature();//获取方法签名
                String name = signature.getName(); //获取方法名
                Object[] args = joinPoint.getArgs();//获取参数
                System.out.println("返回通知,方法的结果是:"+result);//当方法是void时 result=null
    
            }
            //异常通知  在注解中定义异常信息
            @AfterThrowing(value = "pointcut()",throwing = "e") //value属性调用了模板中的value值
            public void afterThrowing(Throwable e){
                System.out.println("发生"+e+"异常");
            }
    
            //环绕通知(可以实现上面的所有通知)
            @Around("execution(public void com.yuing.spring1.aopaspect.AddServer.*(int,int))")
                public Object around(ProceedingJoinPoint joinPoint){
                    String name = joinPoint.getSignature().getName();
                    Object[] args = joinPoint.getArgs();
                    //前置通知
                    System.out.println("方法的名称:"+name+"   方法的参数:"+ Arrays.toString(args));
                    Object result = null;
                    try {
                        result = joinPoint.proceed();
                        System.out.println("返回通知,方法的结果是:"+result);//当方法是void时 result=null
    
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                        //异常通知
                        System.out.println("发生"+throwable+"异常");
                    }
                    //后置通知
                    System.out.println("执行方法之后");
                    return result;
    
                }
    }
    
    - 使用xml文件配置代理
    <!--配置切面类-->
    <bean id="Logging" class="com.yuing.spring1.aopaspect.Logging"></bean>
    <!--配置要进行切面的类-->
    <bean id="addServer" class="com.yuing.spring1.aopaspect.AddServer"/>
    <!--配置切面-->
    <aop:config>
        <!--设置要进行切面的类-->
        <aop:pointcut id="pointcut" expression="execution(* com.yuing.spring1.aopaspect.AddServer.*(..))"/>
        <aop:aspect ref="Logging" order="1"><!--设置切面类和先后顺序-->
            <aop:before method="beform" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
            <!--<aop:around method="around"  pointcut-ref="pointcut"/>-->
        </aop:aspect>
    </aop:config>
    
  • 相关阅读:
    【shell脚本】系统硬件信息数据库收集(普通版和导入Excel版)auto_system_collect.sh
    【linux命令】sz、rz命令安装及使用
    【shell脚本】全备份和增量备份Linux系统脚本auto_bak_system.sh
    【shell脚本】截取恶意端口ip,禁止远程登录22端口auto_deny_ip.sh
    【shell脚本】自动生成网卡文件主要内容auto_network_scripts.sh
    java 多个数 组合成不同的组
    linux jdk配置
    ubuntu sh脚本激活conda 虚拟环境
    liunx mysql数据库目录迁移
    liunx 定时任务执行java程序配置流程
  • 原文地址:https://www.cnblogs.com/yuing/p/8966908.html
Copyright © 2020-2023  润新知