Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自动连线储存库为空_Java_Spring Boot_Repository_Autowired - Fatal编程技术网

Java 自动连线储存库为空

Java 自动连线储存库为空,java,spring-boot,repository,autowired,Java,Spring Boot,Repository,Autowired,我试图在名为CacheManager的类中使用存储库。此存储库应获取表中的所有行。尽管使用了@Autowired注释,它还是会变为null。我在哪里失踪?谢谢 存储库 主静止控制器 因为您使用了私有CoreController core=newCoreController()、CoreController和CacheManager不是Spring管理的bean,因此不会发生依赖项注入 更新 我建议您采用这种方法: @Component public class CacheManager {

我试图在名为CacheManager的类中使用存储库。此存储库应获取表中的所有行。尽管使用了@Autowired注释,它还是会变为null。我在哪里失踪?谢谢

存储库 主静止控制器
因为您使用了私有CoreController core=newCoreController()、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());
        }
    }
}
@组件
公共类缓存管理器{
专用长秒=1000L;
私人长分钟=秒*60;
私人长时间=分钟*60;
专用长TTL=小时;
@自动连线
私有FraudCacheListRepository FraudCacheListRepository;
@施工后
public void getAllTables(){
List fraudCacheListEntities=fraudCacheListRepository.findAll();
for(FraudCacheListEntity实体:FraudCacheListEntity){
System.out.println(entity.toString());
}
}
}

它不适用于您的原因是在注入发生之前调用了构造函数。
@PostConstruct
注释指示Spring在bean完全初始化后调用
getAllTables
方法

因为您使用了
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());
        }
    }
}
@组件
公共类缓存管理器{
专用长秒=1000L;
私人长分钟=秒*60;
私人长时间=分钟*60;
专用长TTL=小时;
@自动连线
私有FraudCacheListRepository FraudCacheListRepository;
@施工后
public void getAllTables(){
List fraudCacheListEntities=fraudCacheListRepository.findAll();
for(FraudCacheListEntity实体:FraudCacheListEntity){
System.out.println(entity.toString());
}
}
}

它不适用于您的原因是在注入发生之前调用了构造函数。
@PostConstruct
注释指示Spring在bean完全初始化之后调用
getAllTables
方法

请记住,字段注入发生在构造函数完全执行之后。因此,
CacheManager.getAllTables()
将产生一个NullPointerException,因为字段
fraudCacheListRepository
未初始化。建议:从字段注入更改为构造函数注入。我更改了代码,但构造函数中仍然存在问题:public CacheManager(){getAllTables();}这将在存储库依赖项之前作为依赖项注入。对于需要创建@PostConstructor private void init(){getAllTables();}并在构造函数中删除它的修复,如果这样做有效,请投票认为有用,否则请告诉我。请记住,字段注入发生在构造函数完全执行之后。因此,
CacheManager.getAllTables()
将产生一个NullPointerException,因为字段
fraudCacheListRepository
未初始化。建议:从字段注入更改为构造函数注入。我更改了代码,但构造函数中仍然存在问题:public CacheManager(){getAllTables();}这将在存储库依赖项之前作为依赖项注入。对于需要创建@PostConstructor private void init(){getAllTables();}并在构造函数中删除它的修复,如果这样做有效,请投票认为有用,否则请告诉我。问候
@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;
}
@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());
        }
    }
}