Reputation: 19729
I have two projects A and B. Both are built with Maven, and project A has a Maven dependency to project B. Both project have a class with @Configuration annotation where I define @Beans.
I have beans in project A, from both projects. If I use the @Autowired annotation in project A of a bean that is defined in same project, the autowiring works. However, if I use the @Autowired annotation in project A of a bean from project B, I will get an exception.
What does this mean? How can I autowire a bean in project A, that is defined in project B?
Upvotes: 2
Views: 14047
Reputation: 793
This is normally an issue with the base classpath on ComponentScan.
If you for example have the following base packages
com.myproject.a
and
com.myproject.b
in your project A and B respectively, and you're using SpringBoot with the main class
package com.myproject.a
@Configuration
@EnableAutoConfiguration
@ComponentScan
class MyApp {
// Some public static void main ...
}
it will only find your classes in the package com.myproject.a and it's children.
To solve this issue you must enhance the @ComponentScan in a way that it scans both package structures, eg.
package com.myproject.a
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.myproject.a", "com.myproject.b"}
// or basePackages = "com.myproject" in this example
class MyApp {
// Some public static void main ...
}
Upvotes: 7