zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    Miller_Rabin
    无向图必经点、必经边的相关问题
    无向图的连通性与相关问题
    HNOI2012 矿场搭建 v-DCC缩点+分类讨论
    冗余路径 Redundant Paths e-DCC缩点
    poj1275 Cashier Employment 差分约束
    csp2019游记
    picxivic爬虫
    水贴自动机
    三维生命游戏
  • 原文地址:https://www.cnblogs.com/yuing/p/8966908.html
Copyright © 2011-2022 走看看