请教一个 Spring 的问题: MethodValidationPostProcessor 是不是和 Scheduled 冲突呀

ManjusakaL 7月前 100

刚刚提了一个 issue

然后为了方便老哥们看,我直接复制过来

I use the Spring Boot v2.0.5.RELEASE which depends on Spring 5.0.9 Release

I got a problem when I need both MethodValidationPostProcessor and Scheduled

here's the detail.

When I just use Scheduled like below

@SpringBootApplication
@EnableConfigServer
@EnableScheduling
public class SpringConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringConfigServerApplication.class, args);
    }

    @Scheduled(fixedDelay = 5000)
    public void test() {
        System.out.println("Demo");
    }
}

the scheduler works well as what I want. But when I add a MethodValidationPostProcessor like below

@SpringBootApplication
@EnableConfigServer
@EnableScheduling
public class SpringConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringConfigServerApplication.class, args);
    }

    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setProxyTargetClass(true);
        return methodValidationPostProcessor;
    }

    @Scheduled(fixedDelay = 5000)
    public void test() {
        System.out.println("Demo");
    }
}

the scheduler doesn't work. I have already checked the org.springframework.scheduling.config.ScheduledTaskRegistrar, the scheduler inits as well, but there isn't any task in all of the task variables such as fixedRateTasks, fixedDelayTasks and etc.

Is there any chance that we can use MethodValidationPostProcessor and the scheduler together?

最新回复 (8)
  • chendy 7月前
    引用 2
    目测是一个 bug
    但是只要把那个方法挪到别的类里就能正常工作了
    另外 SpringBoot 有自动配置一个`MethodValidationPostProcessor`
  • 楼主 ManjusakaL 7月前
    引用 3
    @chendy 其余的类我试过貌似也不可以
  • 楼主 ManjusakaL 7月前
    引用 4
    @chendy 试了下,移动到其余的类是可以的,不过不能用 Autowired 注入。。
  • 楼主 ManjusakaL 7月前
    引用 5
    @chendy 查到原因了,官方的回复

    the post-processor method should be static to prevent early initialization of the whole chain. You also should have a message in the log stating that some beans couldn't be post-processed. This is the reason why scheduling doesn't work with such arrangement. If moving it to static doesn't fix the problem, please share a small sample (zip or github repo) we can run ourselves and we can reconsider.
  • zhazi 7月前
    引用 6
    应该是生命周期处理上出现问题,比 post processor 先行注册了,导致生命周期组件出现问题
  • 楼主 ManjusakaL 7月前
    引用 7
    @zhazi 嗯,有可能,具体我还得看看文档
  • Aruforce 7月前
    引用 8
    Postprocessor 好像有个 order 接口指定执行顺序…你可以看下 AbstractApplicationContext 这个类的 refresh 的代码…是对象工厂初始化流程代码…beanfactorypost processor 和 Bean Post processor…好像都有 ordered 接口…
  • 楼主 ManjusakaL 7月前
    引用 9
    @Aruforce 我去看看,谢谢老哥
  • 游客
    10
返回