I'd like to autowire a CrudRespository<Type,Key> in an abstract parent class, then use it with the child classes. Error tells me:
java.lang.IllegalStateException: Failed to load ApplicationContext
[...]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountExtractor': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.repository.CrudRepository<com.finnwa.adwords.adconnect.Account, java.lang.Long>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Abstract parent defining the dependency.
@Component
public abstract class Extractor<T,KEY> {
@Autowired
protected CrudRepository<T,KEY> repository;
// some business logic
}
Child class providing the parameters.
@Component
class AccountExtractor extends Extractor<Account, Long>{
// some config
}
Other classes that might be relevant:
public interface AccountRepository extends CrudRepository<Account, Long>{}
@SpringBootApplication
@EnableJpaRepositories(basePackages = "package.my")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
I learned from other questions that the dependency in the parent class may not be private. So I made it protected. Anything I'm missing?
EDIT: So Paul Janssens and M. Deinum have posted some nice workarounds. But why doesn't this work? What is going wrong here?