我嘗試將 javax.validation.constraints.NotNull 與 org.springframework.validation.beanvalidation.MethodValidationPostProcessor.MethodValidationPostProcessor 一起使用,但未在同一類中按預(yù)期運行。拜托,你能告訴我為什么嗎?方法驗證配置類:@Configuration@ComponentScan({ "com.mypackage" })public class MethodValidationConfig { @Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); }}驗證實用程序類:@Component@Validated@Slf4jpublic class ValidationUtils { public void test1(@NotNull String[] test) { if(test == null) { log.error("Test is null!!!"); } } public void test2() { test1(null); }}測試應(yīng)用類:@EnableAsync@EnableTransactionManagement@SpringBootApplication@Slf4jpublic class TestApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(TestApplication.class, args); ValidationUtils validationUtils = context.getBean(ValidationUtils.class); try { validationUtils.test1(null); } catch (Exception e) { log.info("OK"); } try { validationUtils.test2(); } catch (Exception e) { log.info("OK"); } }}com.mypackage.TestApplication:好的 com.mypackage.validationUtils:測試為空?。?
1 回答

小唯快跑啊
TA貢獻(xiàn)1863條經(jīng)驗 獲得超2個贊
我假設(shè)發(fā)生以下情況:當(dāng)您調(diào)用 test1() 時,代理攔截并進(jìn)行驗證。當(dāng)您調(diào)用 test2() 時,它只是將調(diào)用委托給實際對象,其中不涉及代理。
添加回答
舉報
0/150
提交
取消