2012-09-25 40 views
2

我對Spring AOP很熟悉,雖然在這方面沒有太多的經驗。在Spring AOP中指定切入點

我的問題是,如果我想有一類的一些方法的一些AOP功能,並非所有的,那麼可以將它有可能與單一的切入點。我說,我有四個方法保存1,保存2,GET1和get2在我的課,我想在SAVE1應用AOP和SAVE2只,那麼在這種情況下,我怎麼可以創建一個單一的切入點?我的切入點表達式如何看起來像?或者甚至有可能嗎?

回答

1

您需要指定一個切入點表達式來選擇應用您的建議的方法。

請參閱7.2.3在the Spring Documentation中聲明一個切入點,並使用執行連接點標識符來選擇這些方法。

1

有一個切入點表達式,像這樣應該做的伎倆

**execution(* save*(..))** 

有關更多信息,請參見here

2

有很多方法可以做到這一點(使用通配符表達,使用AspectJ註解,..) 我會舉個方面的例子J

class MyClass{ 
      @MyPoint 
      public void save1(){ 
      } 

      @MyPoint 
      public void save2(){ 
      } 

      public void save3(){ 
      } 

      public void save4(){ 
      } 

} 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface MyPoint { 
} 

@Aspect 
@Component 
public class MyAspect { 
    @Before("@annotation(com.xyz.MyPoint)") 
    public void before(JoinPoint joinPoint) throws Throwable { 
     //do here what u want 
    } 

} 

所以你們都設定了,只要你標記爲@Mypoint註釋,spring將在此方法的aspect之前調用,確保spring正在管理此方法和對象,而不是您。包括AspectJ的在你的classpath

0

您可以使用orand與切入點表達式:

execution(* my.Class.myMethod(..)) or execution(* my.Class.myOtherMethod(..)) 
+0

Actally或沒有工作,只好用||而不是 execution(* my.Class.myMethod(..))||執行(* my.Class.myOtherMethod(..)) – Anand