I'm working on an application we'll call MyApp. Previously each application at my workplace had its own set of standard services and repositories; what I'm trying to do is move those services and repositories to a JAR we'll call MyServices hosted on GitHub packages that applications can import, so that when changes are made to services, the parent applications can simply update the JAR version rather than each copying and pasting the changes in.
I created a test environment in MyServices that works fine. The problems come when I try to import MyServices into MyApp and run it. I just can't figure out how to correctly scan the services with Spring. I'm getting many errors for nearly all of my services such as:
Field userRepository in com.mycompany.myservices.backend.security.LoginSuccessHandler required a bean of type 'com.mycompany.myservices.backend.data.repositories.UserRepository' that could not be found.
, when UserRepository definitely is defined in the package, or:
Error creating bean with name 'com.mycompany.myApp.ui.views.admin.LoginView': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mycompany.myservices.backend.data.services.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
when UserService does 100% exist, or others claiming that:
Parameter 0 of constructor in com.mycompany.myservices.backend.data.AppDB required a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' that could not be found.
, when I do actually have a MongoTemplate bean defined in the main myApp package. It seems like Spring isn't scanning things in the correct order, and I'm not sure how to fix it.
Here's my Application.java:
@SpringBootApplication(
exclude = {ErrorMvcAutoConfiguration.class, SecurityAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class},
scanBasePackages = {"com.mycompany.myservices.backend"})
@EnableMongoRepositories(basePackageClasses = { PrimaryMongoConfig.class, SecondaryMongoConfig.class })
@EntityScan(basePackageClasses = { User.class })
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
To reiterate, this setup works perfectly fine in applications that contain their own copies of the services. It just isn't scanning correctly when they're in the JAR dependency, and I'm not sure why, since it should be scanned via the scanBasePackages. Ideas?