There are a few things wrong (or rather misunderstood) in your question.
With your current context, you are declaring two bean definitions for each of A
and ServiceFactory
. One implicitly from the @Component
annotation and <component-scan>
and another from the explicit <bean>
declarations. Choose one or the other or you might find yourself in a position where Spring doesn't know which to use.
With the above, what happens is that Spring tries to generate an A
bean because of its @Component
annotation and sees @Required
on the setServiceFactory()
method, but doesn't know what to do with it. @Required
's javadoc states
Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.
But in your case, it isn't. Add @Autowired
to it.
@Required
@Autowired
public void setServiceFactory(ServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
}
Now Spring will know what to do with that method, ie. inject a ServiceFactory
bean.