I have an interface that is implemented by multiple classes, and these classes also have some extra methods. If I autowire the class. Is there a way I can autowire these extra methods?
Example-
Interface
JavaScript
x
public interface ExampleInterface {
String methodOne();
String methodTwo();
}
Service 1
JavaScript
@Service
public class ServiceOne implements ExampleInterface {
@Override
String methodOne() {
return "one";
}
@Override
String methodTwo() {
return "two";
}
String methodThree() {
return "three";
}
}
Service 2
JavaScript
@Service
public class ServiceTwo implements ExampleInterface {
@Override
String methodOne() {
return "1";
}
@Override
String methodTwo() {
return "2";
}
String otherMethod() {
return "hello";
}
}
If I try to directly Autowire the service like this-
JavaScript
@Service
public class Service{
ServiceOne serviceOne;
@Autowired
public Service(ServiceOne serviceOne) {
this.serviceOne = serviceOne;
}
}
then I am getting error something like this-
JavaScript
Parameter 0 of constructor in Service required a bean of type 'ServiceOne' that could not be found.
Advertisement
Answer
I was using @ConditionalOnProperty on the services, that is why it was not getting autowired.