简体   繁体   English

将@Configuration类从一个项目导入到另一个项目

[英]Import @Configuration class from one project to another

I have to projects: ProjectA and ProjectB. 我必须进行项目:ProjectA和ProjectB。

In ProjectA I have: 在ProjectA中,我有:

    package somePackageOfProjectA;
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = { "bla.bla" })
    public class ProjectAConfig extends WebMvcConfigurerAdapter { 
    ..
    }

In the classpath of ProjectB I've added ProjectA and I have the next configuration: 在ProjectB的类路径中,我添加了ProjectA,并具有以下配置:

    package somePackageOfProjectB;
    @Configuration
    @EnableWebMvc
    @Import(ProjectAConfig .class)
    @ComponentScan(basePackages = { "bla.bla" })
    public class ProjectBConfig extends WebMvcConfigurerAdapter { 
    ..
    }

Until now all good, without any compilation error (ProjectAConfig is recognized in ProjectB). 到目前为止,一切都很好,没有任何编译错误(ProjectAConfig在ProjectB中可以识别)。

The problem appear when I start the server. 启动服务器时出现问题。 For an unknown reason is trying to search somePackageOfProjectA.ProjectAConfig only in ProjectB without searching it in ProjectA and an error occurs because is not existing. 由于未知原因,试图仅在ProjectB中搜索somePackageOfProjectA.ProjectAConfig而不在ProjectA中搜索它,并且由于不存在而发生错误。

Any ideas? 有任何想法吗? Thank you in advance! 先感谢您!

I can't be sure, but it feels wrong to me to have two classes that extend WebMvcConfigurerAdapter inside the same web application, what did you expect Spring to do, merge the list of ViewResolvers and HttpMessageConverters? 我不确定,但是在同一个Web应用程序中有两个扩展WebMvcConfigurerAdapter的类对我来说是错误的,您希望Spring做什么,合并ViewResolvers和HttpMessageConverters的列表?

If you have beans that you want to reuse between projects, I would suggest that you put them into a separate @Configuration class instead og the class that extends WebMvcConfigurerAdapter. 如果您有要在项目之间重用的bean,建议您将它们放在单独的@Configuration类中,而不要放在扩展WebMvcConfigurerAdapter的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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