zoukankan      html  css  js  c++  java
  • Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程

     

    在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业。

     

    需要在bean.xml中这样写:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.bjsxt"/>

         <aop:aspectj-autoproxy />
      

    </beans>

     

    使用aspectJ需要导入aspectj的jar包 aspectjrt(runtime)和aspectjweave(编织)

    如果被代理的类没有实现接口还要导入cglib-nodep的jar包,它可以修改字节码来实现代理。

     

    在代码中的写法:

    package com.bjsxt.aop;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;

    @Aspect
    @Component//这个注解不能少,这样spring才会将其识别为组件
    public class LogInterceptor {
        @Pointcut("execution(public * com.bjsxt.service..*.add(..))")
        public void myMethod(){};//这个method相当于上面定义Pointcut的name,写法比较特殊,但是aspectj就这么搞的。
       
        @Before("myMethod()")//这里的myMethod就相当于Pointcut所指的内容了("execution(public * com.bjsxt.service..*.add(..))")。
        public void before() {
            System.out.println("method before");
        }
       
        @Around("myMethod()")
        public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("method around start");
            pjp.proceed();//表示放行,可以执行其他拦截器,没有就执行方法本身
            System.out.println("method around end");
        }
       
    }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    附几种Pointcut写法:

     

    package com.xyz.someapp;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class SystemArchitecture {
    
      /**
       * A join point is in the web layer if the method is defined
       * in a type in the com.xyz.someapp.web package or any sub-package
       * under that.
       */
      @Pointcut("within(com.xyz.someapp.web..*)")
      public void inWebLayer() {}
    
      /**
       * A join point is in the service layer if the method is defined
       * in a type in the com.xyz.someapp.service package or any sub-package
       * under that.
       */
      @Pointcut("within(com.xyz.someapp.service..*)")
      public void inServiceLayer() {}
    
      /**
       * A join point is in the data access layer if the method is defined
       * in a type in the com.xyz.someapp.dao package or any sub-package
       * under that.
       */
      @Pointcut("within(com.xyz.someapp.dao..*)")
      public void inDataAccessLayer() {}
    
      /**
       * A business service is the execution of any method defined on a service
       * interface. This definition assumes that interfaces are placed in the
       * "service" package, and that implementation types are in sub-packages.
       *
       * If you group service interfaces by functional area (for example,
       * in packages com.xyz.someapp.abc.service and com.xyz.def.service) then
       * the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))"
       * could be used instead.
       *
       * Alternatively, you can write the expression using the 'bean'
       * PCD, like so "bean(*Service)". (This assumes that you have
       * named your Spring service beans in a consistent fashion.)
       */
      @Pointcut("execution(* com.xyz.someapp.service.*.*(..))")
      public void businessService() {}
    
      /**
       * A data access operation is the execution of any method defined on a
       * dao interface. This definition assumes that interfaces are placed in the
       * "dao" package, and that implementation types are in sub-packages.
       */
      @Pointcut("execution(* com.xyz.someapp.dao.*.*(..))")
      public void dataAccessOperation() {}
    
    }
  • 相关阅读:
    Springboot(一)springboot简介与入门程序
    Java基础(二)流程语句与数组
    Java基础(一)基础常识
    Kubernetes介绍
    K8s 存储
    Kubernetes 集群组件
    Kubernetes 设计理念
    Kubernetes 部署与实战
    容器技术与资源隔离
    Docker 网络配置
  • 原文地址:https://www.cnblogs.com/flying607/p/3485421.html
Copyright © 2011-2022 走看看