Spring AOP:@annotation(注释)

问题描述 投票:7回答:4

我(当然)试图使用许多我不太了解的结构来维护项目。在试图弄清楚Spring中的AOP使用的过程中,我遇到了带有以下注释的方法:

@Around(value =“@ annotation(annotation)”)

所以@Around意味着我们在AOP中执行方法切入点的'around'版本,我明白了。我不知道其他部分是什么意思。 Spring文档提供以下内容:

@annotation - 限制连接点的匹配,其中连接点的主题(在Spring AOP中执行的方法)具有给定的注释

我不知道这意味着什么 - “在Spring AOP中执行的方法”听起来像建议的方法,但我不知道我(或Spring)如何找出建议的方法。听起来它是具有“给定注释”的方法,但如果是这样,那么给出了什么注释?

这个注释建议了哪些方法?还有什么意思呢?

java spring annotations aspectj aop
4个回答
14
投票

如果您有以下Spring Bean:

@Component
public class foo {

    @com.pkg.Bar      
    void fooMe() {
    }
}

然后是以下建议:

@Around("@annotation(com.pkg.Bar)")

将调用围绕fooMe(或任何其他使用@Bar注释的Spring bean方法)的拦截器

@Transactional注释就是一个很好的例子


1
投票

您将拥有一个名为annotation的参数,其类型相应。它被称为绑定注释,请参阅Spring AOP documentation的摘录:

以下示例显示了如何匹配使用@Auditable注释注释的方法的执行,并提取审计代码。

首先是@Auditable注释的定义:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auditable {
    AuditCode value();
}

然后是与@Auditable方法的执行相匹配的建议:

@Before("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(auditable)")
public void audit(Auditable auditable) {
    AuditCode code = auditable.value();
    // ...
}

0
投票
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
public class TimeLoggingAspect {

@Before("timeLoggingAspect()")
public void logBefore(JoinPoint joinPoint){
    System.out.println("Method Name="+joinPoint.getSignature().getName());
    System.out.println("Logging Before...");
}

/*
// Other way for AOP implemetation 
@Pointcut("execution(public void userService())")
  public void timeLoggingAspect(){
}

@After("timeLoggingAspect()")
public void logAfter() throws Exception{
    System.out.println("Logging After...");
    throw new Exception("Error after Logging");
}

@AfterThrowing(pointcut="timeLoggingAspect()",throwing="exception")
public void logAfterThrowingException(Exception exception){
  System.out.println(exception.getLocalizedMessage());
}*/
}


 /** Config class **/
 import org.springframework.stereotype.Component;
 import com.annotation.EnableMethodVisit;
 @Component
 @EnableMethodVisit
 public class UserService {

    public void userService(){
    System.out.println("user service calling......");
  }
 }

 /** Custom Annotation are used **/
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;

 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 public @interface EnableMethodVisit {

 }


 /** UserService **/
 import org.springframework.stereotype.Component;

 import com.annotation.EnableMethodVisit;

 @Component
 @EnableMethodVisit
 public class UserService {
    public void userService(){
        System.out.println("user service calling......");
    }
 }

 /** AOP Test **/

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.aop.classes.UserService;
 public class SpringAopTest {

 public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
       ctx.register(AspectConfig.class);
       ctx.refresh();
       UserService userService = ctx.getBean(UserService.class);
       userService.userService();;
    }
  }

0
投票

如果您有以下Spring Bean:

@Component
public class foo {
    @com.pkg.Bar      
    void fooMe() {
    }
}

以及@interface:

public @interface Bar {

    String value() default "default value";
}

你可以使用以下建议:

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

@Aspect
@Component
public class BarAop {

    @Around(value = "@annotation(bar)") // This 'bar' is one of the parameters of method around(point, bar)
    public Object around(ProceedingJoinPoint point, Bar bar) throws Throwable {

        String value = bar.value();
        System.out.println(value); // will print "default value"

        // execute target method
        Object object = point.proceed();
        System.out.println("return : " + object);

        return object;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.