• spring切面编程


     xml配置实现

    先写三个类

    public String amethod(String s) {
      System.out.println("This is AAAAAAAAAAAAAAAA");
      return "This is A return :"+s;
     }
    public class B { public void bmethod() { System.out.println("This is BBBBBBBBBBBBBBBBBBBBB "); } }
    //这个类实现了spring里的接口,在配置文件中配置advisor的bean
    public class C implements AfterReturningAdvice{
    
    
     @Override
     public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
    
    
      System.out.println("This is returnValue:"+returnValue.toString());
      System.out.println("This is method:"+method.getName());
      for (Object arg : args) {
       System.out.println("This is args:"+arg.toString());
      }
      System.out.println("This is target:"+target);
     }
    }
     

    spring配置文件内容

    <bean id="a" class="com.hehe.aop.A" />
     <bean id="b" class="com.hehe.aop.B" />
     <bean id="c" class="com.hehe.aop.C" />
     <aop:config>
      <aop:pointcut id="p" expression="execution(* com.hehe.aop.A.*(..))" />
      <!-- advicor 要实现spring里的接口 -->
      <aop:advisor advice-ref="c" pointcut-ref="p" />
      <!-- 切面,普通类,b的bmethod方法切入定义好的切点位置 -->
      <aop:aspect ref="b">
       <!-- 切点可以写表达式,也可以引用定义好的,下面两种效果一样 -->
       <!-- <aop:before method="bmethod" pointcut="execution(* com.hehe.aop.A.*(..))" /> -->
       <aop:before method="bmethod" pointcut-ref="p" />
      </aop:aspect>
     </aop:config>

    写个执行方法

    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
      A a =  (A) context.getBean("a");
      a.amethod("Hello world");
     }

    执行结果:

    This is BBBBBBBBBBBBBBBBBBBBB        //切点类之前执行
    This is AAAAAAAAAAAAAAAA                     //切点类
    This is returnValue:This is A return :Hello world //下面语句是切点类之后执行结果
    This is method:amethod
    This is args:Hello world
    This is target:com.hehe.aop.A@7fa98a66

    注解实现

    先写俩类

    public class A {
        public String amethod(String s) {
            System.out.println("This is AAAAAAAAAAAAAAAA");
            return "This is A return :"+s;
        }
    }
    
    @Component
    @Aspect
    public class D {
    
        @AfterReturning(value = "execution(* com.hehe.aop.A.*(..))",returning="returnValue")
        public void dmethod(JoinPoint j,Object returnValue) {
            System.out.println(returnValue.toString());
            String name = j.getSignature().getName();
            System.out.println(name);
        }
    }
    
    //好几个joinpoint,用这个:import org.aspectj.lang.JoinPoint;

    配置文件

    <context:component-scan base-package="com.hehe.aop" />
        <aop:aspectj-autoproxy/>//这个必须要有,没有还不报错。
        <bean id="a" class="com.hehe.aop.A" />

    测试类

    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
            A a =  (A) context.getBean("a");
            a.amethod("Hello world");
        }
    }

    测试结果

    This is AAAAAAAAAAAAAAAA
    This is A return :Hello world
    amethod

  • 相关阅读:
    explain详解与索引最佳实践
    MySQL的配置文件
    MySQL索引数据结构红黑树,Hash,B+树详解
    elasticsearch 进阶
    淘宝服务端高并发分布式架构演进之路
    http请求的header的一个小细节
    一次解决idea maven settings.xml文件不生效
    SpringBoot dev-tools vjtools dozer热启动类加载器不相同问题
    spring boot eclipse 远程调试
    vscode 同步配置
  • 原文地址:https://www.cnblogs.com/a-s-m/p/10710051.html
Copyright © 2020-2023  润新知