2013-06-01 62 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 -->