zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    数组的操作
    数据类型的判断
    插入排序法
    数组
    文件路径
    POJ 1149 PIGS(最大流)
    POJ 2186 Popular Cows(强联通+缩点)
    POJ 1463 Strategic game(二分图最大匹配)
    POJ 2761 Feed the dogs(平衡树or划分树or主席树)
    POJ 2528 Mayor's posters(线段树)
  • 原文地址:https://www.cnblogs.com/a-s-m/p/10710051.html
Copyright © 2011-2022 走看看