跨Java包的自动连线Springbean

跨Java包的自动连线Springbean,java,spring,spring-mvc,spring-annotations,Java,Spring,Spring Mvc,Spring Annotations,我试图将我的项目分为三个模块:核心、管理和用户,这样我就可以通过核心共享公共代码。问题是我不能让Spring跨不同的主包拾取自动连线bean,当我的所有东西都在同一个包中时,它就可以工作了 在com.mickeycorp.core包中,我有希望管理员和用户模块使用的模型、服务等。在com.mickeycorp.admin中是my WebApplicationStarter(扩展SpringBootServletializer),其中我有: @Override protected SpringAp

我试图将我的项目分为三个模块:核心、管理和用户,这样我就可以通过核心共享公共代码。问题是我不能让Spring跨不同的主包拾取自动连线bean,当我的所有东西都在同一个包中时,它就可以工作了

在com.mickeycorp.core包中,我有希望管理员和用户模块使用的模型、服务等。在com.mickeycorp.admin中是my WebApplicationStarter(扩展SpringBootServletializer),其中我有:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(SpringConfiguration.class);
    return application.sources(WebApplicationStarter.class);
}
我认为这应该是我的配置类,我有以下内容:

@Configuration
@ComponentScan("com.mickeycorp")
public class SpringConfiguration {

}

很明显我误解了什么。。我以为设置ComponentScan会让Spring扫描com.mickeycorp下的包以获取组件注释?

我的思路是正确的。。添加
@ComponentScan
仅是其中的三分之一,是正确的,但它没有将Spring配置为扫描其他类型-它只包括
@Component
@Repository
@Service
@Controller
注释。我必须将以下内容添加到拾取
@Entity
@Repository

@EntityScan("com.mickeycorp.core")
@EnableJpaRepositories("com.mickeycorp.core")
在这种情况下,重写
SpringApplicationBuilder
也是不必要的,因为
SpringConfiguration
类是自动选取的

参考资料:


必须使用组件扫描注释

请参阅

可以指定
basePackageClasses()
basePackages()
(或其别名
value()
)来定义要扫描的特定包

@ComponentScan(basePackages={"package1","package2"})

WebApplicationStarter,该类是否已注释?什么注释?@StefaanNeyts是的,它只是有
@SpringBootApplication
你有没有忘记把核心模块放在admin和user模块的类路径中?通过Maven依赖项或在管理/用户模块的lib文件夹中。@MystyxMac否,管理/用户模块具有Maven依赖项并正确编译。为什么使用AnnotationConfigWebApplicationContext?您可以尝试使用configure(){return application.sources(新类[]{WebApplication.Class,SpringConfiguration.Class});}是的。。这已在接受的答复中说明。除此之外,还需要
@EntityScan
@enablejparepositions