gpt4 book ai didi

java - 带有反射的 Autowiring 存储库

转载 作者:行者123 更新时间:2023-11-29 08:25:55 24 4
gpt4 key购买 nike

我正在尝试通过反射访问我的存储库,以降低项目的可读性复杂性。关于 cacheTable,我必须缓存 3 个数据库表。这些表是;

I.TEST_PARAMETERS
II.TEST_REAL_TRANSACTION_CRITERIA
三、DEV_USER_STDLOV

如果我不使用反射,我必须使用 switch case 来根据表名使用存储库,例如:

没有反射

@Autowired
private TestCacheListRepository testCacheListRepository;

@Autowired
private TestParametersRepository testParametersRepository;

@Autowired
private TestTransactionCriteriaRepository testTransactionCriteriaRepository;

@Autowired
private DevUserStdlovRepository devUserStdlovRepository;

for (TestCacheListEntity entity : testCacheListEntities) {
String cacheKey = entity.getCacheKey();

switch (cacheKey.toLowerCase()) {
case "test_parameters" :
cachedObject.put("test_parameters", new ArrayList<>());
for (TestParametersEntity testParametersEntity : testParametersRepository.findAll()) {
cachedObject.get("test_parameters").add(testParametersEntity);
}
break;

case "test_real_transaction_criteria":
cachedObject.put("test_real_transaction_criteria", new ArrayList<>());
for (TestRealTransactionCriteriaEntity testRealTransactionCriteriaEntity : testRealTransactionCriteriaRepository.findAll()) {
cachedObject.get("test_real_transaction_criteria").add(testRealTransactionCriteriaEntity);
}
break;

case "devuser_ngi_stdlov":
cachedObject.put("devuser_ngi_stdlov", new ArrayList<>());
for (DevUserStdlovEntity devUserStdlovEntity : devUserStdlovRepository.findAll()) {
cachedObject.get("devuser_ngi_stdlov").add(devUserStdlovEntity);
}
break;
}
}

另一方面,如果我使用反射,我会得到这样的结果,但我遇到了问题。

反射(reflection)

List<TestCacheListEntity> testCacheListEntities = testCacheListRepository.findAll();

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

第一个问题是;

代码跳入 catch block 并说

java.lang.ClassNotFoundException: TestParametersRepository

java.lang.ClassNotFoundException: TestRealTransactionCriteriaRepository

java.lang.ClassNotFoundException: DevUserNgiStdlovRepository

我还需要将这些存储库与@Autowired 注释一起使用。可以这样用吗?

谢谢。

最佳答案

反射是你在这里最不需要的东西,它不会提高可读性,但会使事情变得不必要的复杂、缓慢和难以理解。

不过,您可以通过简单地使用更好的 OO 设计来避免三次重复相同的代码。例如,假设根据您发布的代码,您的 3 个实体扩展相同的基类或实现相同的接口(interface) BaseEntity:

private Map<String, Supplier<Collection<? extends BaseEntity>> suppliersByCacheKey = new HashMap<>();

...

@PostConstruct
void init() {
suppliersByCacheKey.put("fraud_parameters", () -> fraudParametersRepository.findAll());
suppliersByCacheKey.put("fraud_real_transaction_criteria", () -> realTransactionCriteriaRepository.findAll());
suppliersByCacheKey.put("ndvlive_ngi_stdlov", () -> ndvNgiStdlovRepository.findAll());
}
...

for (FraudCacheListEntity entity : fraudCacheListEntities) {
String cacheKey = entity.getCacheKey();
List<BaseEntity> listToCache =
new ArrayList<>(suppliersByCachKey.get(cacheKey).get());
cachedObject.put(cacheKey, listToCache);
}

在传统的业务代码中,反射几乎总是一个糟糕的想法。学习多态性、数据结构、设计模式:这些是您应该使用的工具。

关于java - 带有反射的 Autowiring 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com