AOP注解式拦截

Posted 胖头猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AOP注解式拦截相关的知识,希望对你有一定的参考价值。

1. 自己定义的拦截注解

package com.spring.aop; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.ANNOTATION_TYPE,ElementType.METHOD,ElementType.TYPE}) @Documented
public @interface MyAction { String value(); }
2. 定义aop切点

package com.spring.aop; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect @Component
public class MyAop { //切点切入自己配置的那个注解MyAction @Pointcut("@annotation(com.spring.aop.MyAction)") //注意这里的写法@annotation() 切入注解 public void annotationPointCat(){}

  /*
  @Pointcut("execution(* com.savage.aop.MessageSender.*(..))") //注意这里的写法execution() 切入类
  private void logSender(){}
*/

    
    @Before("annotationPointCat()")//位置为这个包下的所有类、所有方法、不论参数是什么类型
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        MyAction myAction =  method.getAnnotation(MyAction.class);
        System.out.println("before: "+myAction.value());
    }
    
    @After("annotationPointCat()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        MyAction myAction =  method.getAnnotation(MyAction.class);
        System.out.println("after: "+myAction.value());
    }
}
3. 编写服务类,应用切面

package com.spring.aop; import org.springframework.stereotype.Component; @Component
public class MyServer {
  /*
  显而易见,利用注解式拦截,会提高切面的复用率
  */ @MyAction(
"ser01") public void ser01(){ System.out.println("call ser01"); } @MyAction("ser02") public void ser02(){ System.out.println("call ser02"); } }
4. Spring配置文件(APP.xml)

<context:component-scan base-package="com.spring.aop"></context:component-scan> <!-- 使 AspectJ 的注解起作用 ; autoproxy自动代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
5. 运行   

public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("com/spring/ioc/APP.xml"); MyServer myServer = factory.getBean(MyServer.class); myServer.ser01(); myServer.ser02(); }

 

依赖jar包    
<!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${org.aspectj-version}</version> </dependency>

 

异常:
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
是因为缺少 aspectjweaver 包


 

以上是关于AOP注解式拦截的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot AOP注解式拦截与方法规则拦截

Spring--AOP

声明式事务基于注解@Transactional的理解

spring的@Transactional注解

aop 拦截含有特定注解的类

用户操作拦截并作日志记录--自定义注解+AOP拦截