Reputation: 3001
Is it possible to have a Spring Bean implement 2 interfaces and be able to autowire that bean using either interface?
I have the following two interfaces:
public interface ServiceA {}
public interface ServiceB {}
Two controllers which use constructor auto-wiring to inject a different service:
@RestController
public class ControllerA {
public ControllerA(ServiceA service) {}
}
@RestController
public class ControllerB {
public ControllerB(ServiceB service) {}
}
One class that implements both the services
@Service
public class ServiceImpl implements ServiceA, ServiceB { }
I am getting a NoSuchBeanDefinitionException
:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ServiceB] found for dependency [ServiceB]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
I'm using Spring Boot version 1.4.0
Upvotes: 12
Views: 3204
Reputation: 9546
Yes it is possible, but
it is important, to create the service bean of type ServiceImpl
and not as one of the service interfaces :
@Bean
ServiceImpl service() {
return new Serviceimpl();
}
Spring uses reflection on the declared bean type to find out which interfaces it implements and not on bean.getClass()
.
Even if this answer was voted dowen, you can be asured : it works . If it does not work for you @scarba05, your problem must be somewhere else...
Upvotes: 3
Reputation: 1106
Let me answer your questions one by one:
Your shared code working fine just check your SpringBootConfiguration class I think you are not scanning you service package or your service class is not in child package of SpringBootConfiguration class.
That's why you are facing:
NoSuchBeanDefinitionException
Upvotes: 0
Reputation: 1838
You could use the @Qualifier
annotation. It can be applied alongside @Autowired
or @Inject
at the point of injection to specify which bean you want to be injected:
@Autowired
@Qualifier("iceCream")
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
Source: Spring in Action 4th edition.
Upvotes: 1