I have a spring-boot project with 3 maven modules in it: producer, consumer and api.
- Api contains an interface.
- Consumer depends on api and contains the
main
method and@SpringBootApplication
. The class is also in a package that prefixes all the other classes in the other two jars so Component Scan could find everything - Producer depends on api and contains an implementation of the api interface annotated with
@Service
In Consumer, i'm trying to get the producer to be injected in the constructor but without referencing the concrete implementation, just the interface. The consumer maven module doesn't even depend on the producer module. This is similar to the way you create applications in OSGi where concrete provider implementations are supposed to be hidden from their consumers.
My problem is that the producer is not being injected. It is not being instantiated or even its class loaded since nobody is referencing it. How can I accomplish this in spring (boot) while keeping the strong encapsulation requirement of consumers not being aware of concrete producers?
When I run the app I get UnsatisfiedDependencyException
since there's not producer instantiated to be injected
This is a simplified representation of my code
package com.foo.api
public interface Doer {
}
===== different jar =====
package com.foo
@SpringBootApplication
public class Consumer {
public static void main(String[] args) {
SpringApplication.run(Consumer.class, args);
}
@Autowire
public Consumer(Doer someDoer) {
}
}
===== different jar ======
package com.foo.services
@Service
public class Producer implements Doer {
}