自动装配存储库为空
Autowired Repository is Null
我正在尝试在名为 CacheManager 的 class 中使用存储库。这个存储库应该从 table 中获取所有行。尽管使用了@Autowired 注释,它还是为空。我哪里不见了?谢谢
存储库
@Repository
public interface FraudCacheListRepository extends CrudRepository<FraudCacheListEntity,Integer> {
List<FraudCacheListEntity> findAll();
}
缓存管理器
@Component
public class CacheManager {
private long second = 1000L;
private long minute = second * 60;
private long hour = minute * 60;
private long TTL = hour;
@Autowired
private FraudCacheListRepository fraudCacheListRepository;
public CacheManager() {
getAllTables();
}
private void getAllTables(){
List<FraudCacheListEntity> fraudCacheListEntities = fraudCacheListRepository.findAll();
for (FraudCacheListEntity entity:fraudCacheListEntities) {
System.out.println(entity.toString());
}
}
}
核心控制器
@Component
@Configurable
public class CoreController {
public ComController com;
@Autowired
private CacheManager cacheManager;
public CoreController() {
com = new ComController();
}
}
主要 - 休息控制器
@RestController
public class FraudRestController {
@Autowired
private CoreController core;
}
由于您使用了 private CoreController core = new CoreController();
,CoreController 和 CacheManager 不是 Spring 托管 bean,因此不会发生依赖注入。
更新
我建议你这个方法:
@Component
public class CacheManager {
private long second = 1000L;
private long minute = second * 60;
private long hour = minute * 60;
private long TTL = hour;
@Autowired
private FraudCacheListRepository fraudCacheListRepository;
@PostConstruct
public void getAllTables(){
List<FraudCacheListEntity> fraudCacheListEntities = fraudCacheListRepository.findAll();
for (FraudCacheListEntity entity:fraudCacheListEntities) {
System.out.println(entity.toString());
}
}
}
它对您不起作用的原因是构造函数在注入发生之前被调用。 @PostConstruct
注释指示 Spring 在 bean 完全初始化后 调用 getAllTables
方法。
我正在尝试在名为 CacheManager 的 class 中使用存储库。这个存储库应该从 table 中获取所有行。尽管使用了@Autowired 注释,它还是为空。我哪里不见了?谢谢
存储库
@Repository
public interface FraudCacheListRepository extends CrudRepository<FraudCacheListEntity,Integer> {
List<FraudCacheListEntity> findAll();
}
缓存管理器
@Component
public class CacheManager {
private long second = 1000L;
private long minute = second * 60;
private long hour = minute * 60;
private long TTL = hour;
@Autowired
private FraudCacheListRepository fraudCacheListRepository;
public CacheManager() {
getAllTables();
}
private void getAllTables(){
List<FraudCacheListEntity> fraudCacheListEntities = fraudCacheListRepository.findAll();
for (FraudCacheListEntity entity:fraudCacheListEntities) {
System.out.println(entity.toString());
}
}
}
核心控制器
@Component
@Configurable
public class CoreController {
public ComController com;
@Autowired
private CacheManager cacheManager;
public CoreController() {
com = new ComController();
}
}
主要 - 休息控制器
@RestController
public class FraudRestController {
@Autowired
private CoreController core;
}
由于您使用了 private CoreController core = new CoreController();
,CoreController 和 CacheManager 不是 Spring 托管 bean,因此不会发生依赖注入。
更新
我建议你这个方法:
@Component
public class CacheManager {
private long second = 1000L;
private long minute = second * 60;
private long hour = minute * 60;
private long TTL = hour;
@Autowired
private FraudCacheListRepository fraudCacheListRepository;
@PostConstruct
public void getAllTables(){
List<FraudCacheListEntity> fraudCacheListEntities = fraudCacheListRepository.findAll();
for (FraudCacheListEntity entity:fraudCacheListEntities) {
System.out.println(entity.toString());
}
}
}
它对您不起作用的原因是构造函数在注入发生之前被调用。 @PostConstruct
注释指示 Spring 在 bean 完全初始化后 调用 getAllTables
方法。