From 9a3c482a680cfa8f857dd501178351b85f421d98 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Mon, 13 May 2024 22:02:05 +0800 Subject: [PATCH] Update I18nBeanPostProcessor.java --- .../beans/factory/config/I18nBeanPostProcessor.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/microsphere-i18n-spring/src/main/java/io/microsphere/i18n/spring/beans/factory/config/I18nBeanPostProcessor.java b/microsphere-i18n-spring/src/main/java/io/microsphere/i18n/spring/beans/factory/config/I18nBeanPostProcessor.java index fa18e16..5d4f461 100644 --- a/microsphere-i18n-spring/src/main/java/io/microsphere/i18n/spring/beans/factory/config/I18nBeanPostProcessor.java +++ b/microsphere-i18n-spring/src/main/java/io/microsphere/i18n/spring/beans/factory/config/I18nBeanPostProcessor.java @@ -9,6 +9,7 @@ import org.springframework.context.MessageSource; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; +import static io.microsphere.spring.util.BeanUtils.getOptionalBean; import static io.microsphere.util.ClassLoaderUtils.resolveClass; import static org.springframework.aop.support.AopUtils.getTargetClass; @@ -46,10 +47,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro Class beanType = getTargetClass(bean); if (LOCAL_VALIDATOR_FACTORY_BEAN_CLASS.equals(beanType)) { - MessageSourceAdapter messageSourceAdapter = context.getBean(MessageSourceAdapter.class); - LocalValidatorFactoryBean localValidatorFactoryBean = (LocalValidatorFactoryBean) bean; - localValidatorFactoryBean.setValidationMessageSource(messageSourceAdapter); - logger.debug("LocalValidatorFactoryBean[name : '{}'] is associated with MessageSource : {}", beanName, messageSourceAdapter); + MessageSourceAdapter messageSourceAdapter = getOptionalBean(this.context, MessageSourceAdapter.class); + if (messageSourceAdapter == null) { + logger.warn("No MessageSourceAdapter BeanDefinition was found!"); + }else{ + LocalValidatorFactoryBean localValidatorFactoryBean = (LocalValidatorFactoryBean) bean; + localValidatorFactoryBean.setValidationMessageSource(messageSourceAdapter); + logger.debug("LocalValidatorFactoryBean[name : '{}'] is associated with MessageSource : {}", beanName, messageSourceAdapter); + } } return bean;