1

我正在尝试对服务层类使用 @Scheduled 注释。该类还通过 AOP 由日志服务监视。

当我让服务类实现一个接口时,Spring会抛出错误

Error creating bean with name 'dummyService' defined in file
......
......
Caused by: java.lang.IllegalStateException: failed to prepare task
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:114)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:452)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:430)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:98)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 11 more
Caused by: java.lang.NoSuchMethodException: $Proxy23.run()
    at java.lang.Class.getMethod(Class.java:1605)
    at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:178)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:111)
    ... 17 more

这是服务层类:

package com.mydomain.web.myapp.service;
@Service
public class DummyService implements DummyI{
        @Scheduled(cron = "${some.cron.time}")
        public void run() {
        }
}

如果我删除继承,它可以正常工作。这是为什么?

这就是我为日志服务所拥有的:

@Component
@Aspect
public class LoggingServiceImpl implements LoggingService {
        private final Logger log = LoggerFactory.getLogger(this.getClass());
        @Around("execution(* com.mydomain.web.myapp..*.*(..))")
        public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        .....
4

1 回答 1

1

您在这里有几个选择:

  • 添加run()DummyI界面

  • 创建另一个只有run()方法(或使用java.lang.Runnable)的接口并实现该接口:

    public class DummyService implements DummyI, Runnable
    
  • 启用基于类 ( ) 的代理

于 2012-05-16T21:16:18.670 回答