7

I’m attempting to break my project up into three modules: core, admin and user so that I can share common code via core. The problem is that I can’t get Spring to pickup the autowired beans across different main packages, when I have everything in the same package it works.

In the com.mickeycorp.core package I have the models, services, etc that I want the admin and user modules to use. In com.mickeycorp.admin is the my WebApplicationStarter (extends SpringBootServletInitializer) where I’ve got:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(SpringConfiguration.class);
    return application.sources(WebApplicationStarter.class);
}

Which I believe should pickup my configuration class where I have the following:

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

}

Clearly I’ve misunderstood something.. I thought that setting ComponentScan would have Spring scan through packages under com.mickeycorp for component annotations?

Clint
  • 327
  • 2
  • 14
  • WebApplicationStarter, is that class annotated? What annotations? – Stefaan Neyts Mar 26 '15 at 06:24
  • @StefaanNeyts Yep, it just has `@SpringBootApplication` – Clint Mar 26 '15 at 07:15
  • 1
    Did you forget to put the core module in the classpath of admin and user module by any chance? By Maven dependency or in a lib folder in admin/user module. – MystyxMac Mar 26 '15 at 08:56
  • @MystyxMac No, admin/user modules have the Maven dependency and compile correctly. – Clint Mar 26 '15 at 16:36
  • Why use AnnotationConfigWebApplicationContext? Can you try to use configure() { return application.sources( new Class[] { WebApplication.class, SpringConfiguration.class } ); } – MystyxMac Mar 27 '15 at 08:30

2 Answers2

6

I was on the right track.. adding @ComponentScan was only a third of the way there and is correct but it doesn't configure Spring to scan for other types - it only covers @Component @Repository, @Service, or @Controller annotations. I had to add the following to pickup @Entity and @Repository:

@EntityScan("com.mickeycorp.core")
@EnableJpaRepositories("com.mickeycorp.core")

Overriding SpringApplicationBuilder is also unnecessary in this case as the SpringConfiguration class is automatically picked up.

References:

Spring Docs: Entity Scan

Spring Docs: EnableJpaRepositories

Clint
  • 327
  • 2
  • 14
2

@ComponentScan annotation has to be used.

Please refer the Spring documentation which says

Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan.

@ComponentScan(basePackages={"package1","package2"})
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Harish Kumar
  • 316
  • 3
  • 11
  • Yeah.. this is already stated in the accepted answer. Along with that `@EntityScan` and `@EnableJpaRepositories` are also required. – Clint Apr 07 '17 at 16:53