我想CrudRespository<Type,Key>在抽象父类中自动装配 a ,然后将它与子类一起使用。错误告诉我:
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)}
定义依赖项的抽象父级。
@Component
public abstract class Extractor<T,KEY> {
@Autowired
protected CrudRepository<T,KEY> repository;
// some business logic
}
提供参数的子类。
@Component
class AccountExtractor extends Extractor<Account, Long>{
// some config
}
其他可能相关的类:
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);
}
}
我从其他问题中了解到,父类中的依赖项可能不是私有的。所以我把它保护起来。我缺少什么吗?
编辑:所以 Paul Janssens 和 M. Deinum 发布了一些不错的解决方法。但为什么这不起作用?这里出了什么问题?
慕仙森
泛舟湖上清波郎朗
jeck猫
相关分类