1

I am trying to use repository in a class which named CacheManager. This repository should get all rows from the table. Despite using @Autowired annotation, it gets null. Where am I missing? Thanks.

REPOSITORY

@Repository
public interface FraudCacheListRepository extends CrudRepository<FraudCacheListEntity,Integer> {
    List<FraudCacheListEntity> findAll();
}

Cache Manager

@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());
        }
    }
}

Core Controller

@Component
@Configurable
public class CoreController {
    public ComController com;
    @Autowired
    private CacheManager cacheManager;

    public CoreController() {
        com = new ComController();
    }
}

MAIN - Rest Controller

@RestController
public class FraudRestController {
    @Autowired
    private CoreController core;
}
Berkin
  • 1,565
  • 5
  • 22
  • 48
  • Keep in mind, that field injection happens after the constructor is completely executed. So `CacheManager.getAllTables()` will produce a NullPointerException, since the field `fraudCacheListRepository` is not initialized. Suggestion: change from field injection to constructor injection. – Roland Weisleder Nov 02 '18 at 14:39
  • I changed my code but problem still exist – Berkin Nov 02 '18 at 14:41
  • In constructor: public CacheManager() { getAllTables(); } this will injected as dependency before repository dependency. for fix that you need to create @PostConstructor private void init() { getAllTables(); } and remove it in constructor if this works please vote as useful else let me know it. kind regards – Jonathan JOhx Nov 02 '18 at 14:47

1 Answers1

6

Since you used private CoreController core = new CoreController();, CoreController and CacheManager are not Spring managed beans, therefore no dependency injection occurs.

UPDATE

I suggest you this approach:

@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());
        }
    }
}

The reason it's not working for you is that the constructor is called before the injection take place. The @PostConstruct annotation instructs Spring to call getAllTables method after the bean is fully initialized.