lundi 5 novembre 2018

Autowired Repositories With Reflection

I am trying to reach my repositories with reflection to reduce readability complexity in the project. I have to cache 3 DB tables with regarding a cacheTable. These tables are;

I.FRAUD_PARAMETERS
II.FRAUD_REAL_TRANSACTION_CRITERIA
III.NDVLIVE_NGI_STDLOV

If I dont use reflection, I have to use switch case to use repository according to table name such as:

WITHOUT REFLECTION

@Autowired
private FraudCacheListRepository fraudCacheListRepository;

@Autowired
private FraudParametersRepository fraudParametersRepository;

@Autowired
private RealTransactionCriteriaRepository realTransactionCriteriaRepository;

@Autowired
private NdvNgiStdlovRepository ndvNgiStdlovRepository;

for (FraudCacheListEntity entity : fraudCacheListEntities) {
            String cacheKey = entity.getCacheKey();

            switch (cacheKey.toLowerCase()) {
                case "fraud_parameters" :
                    cachedObject.put("fraud_parameters", new ArrayList<>());
                    for (FraudParametersEntity fraudParametersEntity : fraudParametersRepository.findAll()) {
                        cachedObject.get("fraud_parameters").add(fraudParametersEntity);
                    }
                    break;

                case "fraud_real_transaction_criteria":
                    cachedObject.put("fraud_real_transaction_criteria", new ArrayList<>());
                    for (RealTransactionCriteriaEntity realTransactionCriteriaEntity : realTransactionCriteriaRepository.findAll()) {
                        cachedObject.get("fraud_real_transaction_criteria").add(realTransactionCriteriaEntity);
                    }
                    break;

                case "ndvlive_ngi_stdlov":
                    cachedObject.put("ndvlive_ngi_stdlov", new ArrayList<>());
                    for (NdvNgiStdlovEntity ndvNgiStdlovEntity : ndvNgiStdlovRepository.findAll()) {
                        cachedObject.get("ndvlive_ngi_stdlov").add(ndvNgiStdlovEntity);
                    }
                    break;
            }
        }

On the other hand, if I use reflection, I'll get something like this but I am encountering problem with it.

WITH REFLECTION

List<FraudCacheListEntity> fraudCacheListEntities = fraudCacheListRepository.findAll();

        for (FraudCacheListEntity entity : fraudCacheListEntities) {
            String className = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, entity.getCacheKey()) + "Repository";
            try {
                Class c = Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

First problem is;

Code jumps into catch block and says

java.lang.ClassNotFoundException: FraudParametersRepository

or

java.lang.ClassNotFoundException: FraudRealTransactionCriteriaRepository

or

java.lang.ClassNotFoundException: NdvliveNgiStdlovRepository

Also I need to use these repositories with @Autowired annotation. Is it possible to use like this?

Thanks.





Aucun commentaire:

Enregistrer un commentaire