2013-06-01 25 views
4

我想要有一個用@Scheduled批註的方法的AspectJ切入點。嘗試不同的方法,但沒有奏效。帶有@Scheduled Spring批註方法的切入點

1)

@Pointcut("execution(@org.springframework.scheduling.annotation.Scheduled * * (..))") 
public void scheduledJobs() {} 

@Around("scheduledJobs()") 
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable { 
    LOG.info("testing") 
} 

2)

@Pointcut("within(@org.springframework.scheduling.annotation.Scheduled *)") 
public void scheduledJobs() {} 

@Pointcut("execution(public * *(..))") 
public void publicMethod() {} 

@Around("scheduledJobs() && publicMethod()") 
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable { 
    LOG.info("testing") 
} 

任何人都可以提出任何其他方式具有around/before@Scheduled註解的方法的建議嗎?

+0

請使用代碼格式下一次。我剛剛解決了這個問題,但是你已經在這裏呆了很長時間以瞭解它。沒有冒犯的意思。 :-) – kriegaex

回答

3

,你正在尋找可以爲下面指定的切入點:

@Aspect 
public class SomeClass { 

    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)") 
    public void doIt(ProceedingJoinPoint pjp) throws Throwable { 
     System.out.println("before"); 
     pjp.proceed(); 
     System.out.println("After"); 
    } 
} 

我不知道是否這就是你需要與否。所以我要發佈解決方案的其他部分。

首先,請注意類上的註釋@Aspect。這個類的方法需要被應用爲advice

此外,您需要確保具有@Scheduled方法的類可以通過掃描檢測到。您可以通過註釋類來註釋@Component。對於恩:現在

@Component 
public class OtherClass { 
    @Scheduled(fixedDelay = 5000) 
    public void doSomething() { 
     System.out.println("Scheduled Execution"); 
    } 
} 

,對於這項工作,在Spring配置所需的部分將如下所示:

<context:component-scan base-package="com.example.mvc" /> 
<aop:aspectj-autoproxy /> <!-- For @Aspect to work -->  
<task:annotation-driven /> <!-- For @Scheduled to work -->