请教一个 Spring 的问题: MethodValidationPostProcessor 是不是和 Scheduled 冲突呀 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ManjusakaL
V2EX    Java

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

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

    刚刚提了一个 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  
       219-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     Solana     962 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 19:01 PVG 03:01 LAX 11:01 JFK 14:01
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86