2014-02-05 109 views
5

我(當然)試圖用許多構造來維護一個項目,但我不太清楚。在試圖找出在Spring AOP的使用過程中,我碰到的方法來與以下注釋:Spring AOP:@annotation(annotation)

@Around(值=「@annotation(註釋)」)

所以@Around意味着我們」重做AOP中的方法切入點的'around'版本,我明白這一點。我不知道其他部分的含義。 Spring文檔給出如下:

@annotation - 匹配特定的連接點,其中 的主題的連接點(在Spring AOP方法的執行)的限制已給定 註釋

我不不知道這意味着什麼 - 「在Spring AOP中執行的方法」聽起來像是建議的方法,但我不知道我(或Spring)如何確定哪些方法正在被建議。這聽起來像是具有「給定註釋」的方法,但是如果是的話,給出了什麼註釋?

這個註解建議了哪些方法?還有什麼意思?

回答

11

,如果你有以下的Spring bean:

@Component 
public class foo { 

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

那麼以下建議:

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

將調用(與@Bar註釋或任何其他Spring Bean的方法)圍繞fooMe攔截

@Transactional註釋是一個很好的例子

+0

這是有益的,但你有 「@annotation(com.pkg.Bar)」,我有「@annotation(annotation)」,第二個'annotation'本身不是一個特定的註釋 - 關於哪個註釋適用於什麼說法?我也在使用這個類的類中獲得了「public @interface Y」,並且表明這樣定義的註釋是獲得這個建議的那些註解。但是它的規則是什麼? – arcy

+0

在'@annotation(annotation)'註解是括號'(annotation)'只是註釋的全限定類名的佔位符 –

+0

那麼,這是否意味着這個方法可以提供任何註釋? – arcy

0

您將有一個適當類型的名爲annotation的參數。這就是所謂的綁定註釋,看到從Spring AOP documentation此摘錄:

下面的例子展示瞭如何匹配與@Auditable註解 方法執行,並提取審計 代碼。

首先,在@Auditable註解的定義:匹配的@Auditable方法的執行

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

然後建議:

 
@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; 
    } 
}