zoukankan      html  css  js  c++  java
  • Spring(十二)--Spring AspectJ

                   Spring AspectJ

      AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。

      AspectJ 是一个面向切面的框架!定义了AOP的语法!

      Spring 将AspectJ 整合到了自己的框架中!

      需要引入两个核心jar

        01.aspectj.weaver

        02.spring-aspects

      (如果你使用的是idea  所需的所有pom节点,在这儿:https://www.cnblogs.com/fl72/p/9625697.html

      务必掌握的  AspectJ 的切入点表达式

        execution([访问权限类型] 返回值类型 [完整限定类名]方法名 (参数) [抛出的异常类型])

        execution( 返回值类型  方法名(参数))

     

      *:0-N的字符

      ..:

        01.如果是在方法参数中,表示参数可有多个或者可无

        02.如果是在包名之后,表示当前包和子包

      +:

        01.如果定义在类后面,表示当前类以及子类

        02.如果定义在接口后面,表示当前接口以及实现类

    例子:

    1. 使用注解实现

    /**
     * 当前类就是 整个程序中需要的各种系统级业务
     * 就是一个切面类
     */
    @Aspect
    public class MyAspectJ {
    
        @Before("execution(* *..UserDao.sleep(..))")
        public void  before(){
            System.out.println("前置增强........");
        }
    
        @AfterReturning("execution(* *..UserDao.sleep(..))")
        public void  afterReturning(){
            System.out.println("后置增强........");
        }
    //如果想获取方法的返回值
        @AfterReturning(value = "execution(* *..UserDao.sleep(..))",returning = "result")
        public void  afterReturning(String result){
            System.out.println("后置增强........"+result);
        }
    
        /**
         * 环绕增强可以改变返回值
         */
        @Around("execution(* *..UserDao.eat(..))")
        public Object  around(ProceedingJoinPoint point){
            System.out.println("环绕增强进来........");
            Object result=null;
            try {
                 result=  point.proceed(); //执行目标方法
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            System.out.println("环绕增强出去........");
            return  "orange";
        }
    }

    2. 对应的xml文件和测试方法

    <!--01.配置目标对象-->
    <bean id="userDao" class="com.xdf.dao.UserDaoImpl"/>
    
      <!--02.配置切面-->
    <bean id="myAspectJ" class="com.xdf.annotation.MyAspectJ"/>
    
     <!--03.注册aspectj的自动代理-->
    <aop:aspectj-autoproxy/>
    
    //测试方法
    @Test
    public  void  defaultTest(){
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        UserDao dao= context.getBean("userDao", UserDao.class);
      //  System.out.println(dao.eat());
        dao.sleep();
    }

    3. 使用纯切面的方式实现

    public class MyAspectJ {
    
        public void  before(){
            System.out.println("前置增强........");
        }
    
        public void  afterReturning(){
            System.out.println("后置增强........");
        }
    
        /**
         * 环绕增强可以改变返回值
         */
        public Object  around(ProceedingJoinPoint point){
            System.out.println("环绕增强进来........");
            Object result=null;
            try {
                 result=  point.proceed(); //执行目标方法
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            System.out.println("环绕增强出去........");
            return  "orange";
        }
    
    
    }

    4.对应的xml文件和测试方法

    <!--01.配置目标对象-->
    <bean id="userDao" class="com.xdf.dao.UserDaoImpl"/>
    
      <!--02.配置切面-->
    <bean id="myAspectJ" class="com.xdf.annotation.MyAspectJ"/>
    
     <!--03.注册aspectj需要的切入点-->
      <aop:config>
          <!--配置切入点表达式-->
          <aop:pointcut id="myPonit" expression="execution(* *..UserDao.sleep(..))"/>
          <aop:pointcut id="myPonit2" expression="execution(* *..UserDao.eat(..))"/>
          <!--配置切面-->
          <aop:aspect ref="myAspectJ">
              <aop:before method="before" pointcut-ref="myPonit"/>
              <aop:after-returning method="afterReturning" pointcut-ref="myPonit"/>
              <aop:around method="around" pointcut-ref="myPonit2"/>
          </aop:aspect>
      </aop:config>
    @Test
    public  void  aspectJTest(){
        ApplicationContext context=new ClassPathXmlApplicationContext("aspectJ.xml");
        UserDao dao= context.getBean("userDao", UserDao.class);
       //System.out.println(dao.eat());
       dao.sleep();
    }

        未完待续!!!

  • 相关阅读:
    C#生成唯一值的方法汇总
    Ajax && json在springmvc中的使用
    spring集成mybatis
    mybatis iBatis
    springjdbc
    拦截器
    系统分层
    Java反射(reflect)
    springmvc
    spring 之 使用注解简化配置
  • 原文地址:https://www.cnblogs.com/fl72/p/9656229.html
Copyright © 2011-2022 走看看