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

2019-12-09 15:29:17 +08:00
 ManjusakaL

刚刚提了一个 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?

3595 次点击
所在节点    Java
8 条回复
chendy
2019-12-09 16:31:38 +08:00
目测是一个 bug
但是只要把那个方法挪到别的类里就能正常工作了
另外 SpringBoot 有自动配置一个`MethodValidationPostProcessor`
ManjusakaL
2019-12-09 16:58:16 +08:00
@chendy 其余的类我试过貌似也不可以😂😂
ManjusakaL
2019-12-09 17:07:42 +08:00
@chendy 试了下,移动到其余的类是可以的,不过不能用 Autowired 注入。。
ManjusakaL
2019-12-09 19:39:50 +08:00
@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
2019-12-09 19:56:05 +08:00
应该是生命周期处理上出现问题,比 post processor 先行注册了,导致生命周期组件出现问题
ManjusakaL
2019-12-09 20:20:44 +08:00
@zhazi 嗯,有可能,具体我还得看看文档
Aruforce
2019-12-09 20:39:09 +08:00
Postprocessor 好像有个 order 接口指定执行顺序…你可以看下 AbstractApplicationContext 这个类的 refresh 的代码…是对象工厂初始化流程代码…beanfactorypost processor 和 Bean Post processor…好像都有 ordered 接口…
ManjusakaL
2019-12-09 20:48:54 +08:00
@Aruforce 我去看看,谢谢老哥

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/627304

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX