简体   繁体   中英

Beans in a multi-module project

I'm working on a multi-module SpringBoot-powered project where I have modules core and web . I use custom validators for emails, passwords and so on. The validators are defined as a part of the core module. I need some custom feedback messages which I store in the message.properties file, here is a sample definition:

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

I also have the ValidEmail interface which looks the following way:

@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 {};
}

And obviously, I should define a bean of the LocalValidatorFactoryBean which should contain a local validator set up to use the local message source.

Here's what I have in my web module:

@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;
    }

}

(the full version is available here )

It works fine.

But I'd like to move these beans' definition to the core module. I have the following class there:

@SpringBootConfiguration
public class AppConfig {
    // ...skipped...
}

(the full version is available here )

And once I move these two beans' definition to the core module (to the AppConfig class), my validation errors stop being resolved, I get only a message label ( {core.model.validator.EmailValidator.email_invalid} ) instead of English description ( Invalid email address ).

Would anyone be so kind as to tell me what am I doing wrong?

Thanks in advance for your kind help!

PS Here's the link to the full source: https://github.com/melnik13/pass-pass/tree/d24eebfc48610e924308d2da4cd978ac965e2e4e , I'll hope that it helps a bit.

In the javadocs to @SpringBootConfiguration class it is said that

Application should only ever include one @SpringBootConfiguration

So first thing I would check if this core configuration is loaded at all. Probably makes sense to mark the core configuration class with @Configuration instead and use @Import or @ComponentScan in the web configuration class.

Implemented a workaround.

The bean is defined in the core module:

@Configuration
public class CoreModuleConfiguration {

    // ...

    @Bean(name="coreLocalValidatorFactoryBean")
    public LocalValidatorFactoryBean getValidator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(messageSource());
        return localValidatorFactoryBean;
    }

}

(the full version is here )

And then the same bean is mentioned in the web module:

@Configuration
public class WebModuleConfiguration implements WebMvcConfigurer {

    // ...

    private final LocalValidatorFactoryBean localValidatorFactoryBean;

    @Autowired
    public WebModuleConfiguration(
            // ...
            @Qualifier("coreLocalValidatorFactoryBean") LocalValidatorFactoryBean localValidatorFactoryBean
    ) {
        // ...
        this.localValidatorFactoryBean = localValidatorFactoryBean;
    }

    // ...

    @Bean(name = "webLocalValidatorFactoryBean")
    public LocalValidatorFactoryBean getValidator() {
        return this.localValidatorFactoryBean;
    }

}

(the full version is here )

Well, it's a bit ugly, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM