V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ManjusakaL
V2EX  ›  Java

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

  •  
  •   ManjusakaL · 2019-12-09 15:29:17 +08:00 · 3597 次点击
    这是一个创建于 1845 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚刚提了一个 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 条回复    2019-12-09 20:48:54 +08:00
    chendy
        1
    chendy  
       2019-12-09 16:31:38 +08:00
    目测是一个 bug
    但是只要把那个方法挪到别的类里就能正常工作了
    另外 SpringBoot 有自动配置一个`MethodValidationPostProcessor`
    ManjusakaL
        2
    ManjusakaL  
    OP
       2019-12-09 16:58:16 +08:00 via Android
    @chendy 其余的类我试过貌似也不可以😂😂
    ManjusakaL
        3
    ManjusakaL  
    OP
       2019-12-09 17:07:42 +08:00 via Android
    @chendy 试了下,移动到其余的类是可以的,不过不能用 Autowired 注入。。
    ManjusakaL
        4
    ManjusakaL  
    OP
       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
        5
    zhazi  
       2019-12-09 19:56:05 +08:00 via Android
    应该是生命周期处理上出现问题,比 post processor 先行注册了,导致生命周期组件出现问题
    ManjusakaL
        6
    ManjusakaL  
    OP
       2019-12-09 20:20:44 +08:00
    @zhazi 嗯,有可能,具体我还得看看文档
    Aruforce
        7
    Aruforce  
       2019-12-09 20:39:09 +08:00 via Android
    Postprocessor 好像有个 order 接口指定执行顺序…你可以看下 AbstractApplicationContext 这个类的 refresh 的代码…是对象工厂初始化流程代码…beanfactorypost processor 和 Bean Post processor…好像都有 ordered 接口…
    ManjusakaL
        8
    ManjusakaL  
    OP
       2019-12-09 20:48:54 +08:00
    @Aruforce 我去看看,谢谢老哥
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2773 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 06:41 · PVG 14:41 · LAX 22:41 · JFK 01:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.