多模块项目中的 Bean

我正在开发一个由 SpringBoot 驱动的多模块项目,其中有模块core和web. 我对电子邮件、密码等使用自定义验证器。验证器被定义为core模块的一部分。我需要一些存储在message.properties文件中的自定义反馈消息,这是一个示例定义:


core.model.validator.EmailValidator.email_invalid=Invalid email address

我也有如下所示的ValidEmail界面:


@Target({TYPE, FIELD, ANNOTATION_TYPE})

@Retention(RUNTIME)

@Constraint(validatedBy = UserEmailValidator.class)

@Documented

public @interface ValidEmail {

    String message() default "{core.model.validator.EmailValidator.email_invalid}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}

显然,我应该定义一个 bean,LocalValidatorFactoryBean它应该包含一个本地验证器,设置为使用本地消息源。


这是我的web模块中的内容:


@SpringBootConfiguration

public class MvcWebConfig implements WebMvcConfigurer {


    // ...skipped...


    @Bean

    public MessageSource messageSource() {

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

        messageSource.setBasename("classpath:messages");

        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;

    }


    @Bean

    public LocalValidatorFactoryBean getValidator() {

        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();

        localValidatorFactoryBean.setValidationMessageSource(messageSource());

        return localValidatorFactoryBean;

    }


}

(完整版可在此处获得)


它工作正常。


但我想将这些 bean 的定义移到core模块中。我在那里有以下课程:


@SpringBootConfiguration

public class AppConfig {

    // ...skipped...

}

(完整版可在此处获得)


一旦我将这两个 bean 的定义移动到core模块(到AppConfig类)中,我的验证错误就不再得到解决,我只得到一个消息标签({core.model.validator.EmailValidator.email_invalid})而不是英文描述(Invalid email address)。


有人会这么好心告诉我我做错了什么吗?


提前感谢您的帮助!


PS这是完整源代码的链接:https ://github.com/melnik13/pass-pass/tree/d24eebfc48610e924308d2da4cd978ac965e2e4e ,希望对您有所帮助。


白衣非少年
浏览 122回答 2
2回答

慕村9548890

实施了解决方法。bean 在core模块中定义:@Configurationpublic class CoreModuleConfiguration {&nbsp; &nbsp; // ...&nbsp; &nbsp; @Bean(name="coreLocalValidatorFactoryBean")&nbsp; &nbsp; public LocalValidatorFactoryBean getValidator() {&nbsp; &nbsp; &nbsp; &nbsp; LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();&nbsp; &nbsp; &nbsp; &nbsp; localValidatorFactoryBean.setValidationMessageSource(messageSource());&nbsp; &nbsp; &nbsp; &nbsp; return localValidatorFactoryBean;&nbsp; &nbsp; }}(完整版在这里)web然后在模块中提到了相同的bean :@Configurationpublic class WebModuleConfiguration implements WebMvcConfigurer {&nbsp; &nbsp; // ...&nbsp; &nbsp; private final LocalValidatorFactoryBean localValidatorFactoryBean;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; public WebModuleConfiguration(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Qualifier("coreLocalValidatorFactoryBean") LocalValidatorFactoryBean localValidatorFactoryBean&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; this.localValidatorFactoryBean = localValidatorFactoryBean;&nbsp; &nbsp; }&nbsp; &nbsp; // ...&nbsp; &nbsp; @Bean(name = "webLocalValidatorFactoryBean")&nbsp; &nbsp; public LocalValidatorFactoryBean getValidator() {&nbsp; &nbsp; &nbsp; &nbsp; return this.localValidatorFactoryBean;&nbsp; &nbsp; }}好吧,它有点难看,但它确实有效。

眼眸繁星

在javadocs to&nbsp;@SpringBootConfigurationclass中,据说应用程序应该只包括一个@SpringBootConfiguration所以首先我会检查这个核心配置是否已经加载。@Configuration使用@Import或@ComponentScan在 web 配置类中标记核心配置类可能是有意义的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java