【发布时间】:2017-01-05 12:57:31
【问题描述】:
我在初始化 bean 并将 JPA 存储库注入到一个特定的 bean 时遇到了麻烦。不知道为什么它不起作用......
有一个interface定义密钥服务:
public interface KeyService {
Store getKeyStore();
Store getTrustStore();
}
和实现此接口的abstract 类:
public abstract class DefaultKeyService implements KeyService {
abstract KeyRecord loadKeyStore();
abstract KeyRecord loadTrustStore();
/* rest omitted... */
}
以及扩展抽象类的基础class:
@Service
public class DatabaseKeyService extends DefaultKeyService {
@Autowired
private KeyRecordRepository keyRecordRepository;
@Override
protected KeyRecord loadKeyStore() {
return extract(keyRecordRepository.findKeyStore());
}
@Override
protected KeyRecord loadTrustStore() {
return extract(keyRecordRepository.findTrustStore());
}
/* rest omitted... */
}
和bean初始化:
@Bean
public KeyService keyService() {
return new DatabaseKeyService();
}
这是一个KeyRecordRepository 存储库:
public interface KeyRecordRepository extends Repository<KeyRecord, Long> {
KeyRecord save(KeyRecord keyRecord);
@Query("SELECT t FROM KeyRecord t WHERE key_type = 'KEY_STORE' AND is_active = TRUE")
Iterable<KeyRecord> findKeyStore();
@Query("SELECT t FROM KeyRecord t WHERE key_type = 'TRUST_STORE' AND is_active = TRUE")
Iterable<KeyRecord> findTrustStore();
KeyRecord findById(long id);
}
问题:为什么DatabaseKeyService 类中的keyRecordRepository 仍然为空?真的我不知道为什么只有这个字段没有注入。其他 bean 和存储库运行良好。
不会因为父类是抽象类而产生问题吗?
【问题讨论】:
-
用@Component注释KeyRecordRepository的实现
-
KeyRecordRepository没有实现。