[英]Specifying pointcut in Spring AOP
我對Spring AOP很熟悉,盡管沒有太多經驗。
我的問題是,如果我想為某個類的某些方法(而非全部)提供某些AOP功能,那么使用單個切入點是否可以實現? 說,我的類中有四個方法save1,save2,get1和get2 ,並且我只想對save1和save2應用AOP,那么在那種情況下如何為它創建一個切入點? 我的切入點表達式將如何顯示? 甚至有可能嗎?
有很多方法可以做到這一點(通配符表達,aspectJ注釋,..)我將舉一個aspectJ的例子
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
,只要您標記了@Mypoint
批注,spring就會在此方法的方面之前調用,確保spring正在管理此方法和對象,而不是您。 在類路徑中包含AspectJ
您可以將or
和and
與切入點表達式一起使用:
execution(* my.Class.myMethod(..)) or execution(* my.Class.myOtherMethod(..))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.