我可以从枚举访问弹簧组件吗?
我已经完成了从数据库记录创建捆绑包的课程。 记录是从存储过程中获得的,此过程的名称取自ServiceConfig实例,即Spring @configuration。
现在,我使用Java Map使用ServiceConfig方法将每个DB实体类绑定。 我想在此处扩展它并使用枚举而不是procnamesupplier。是否可以从Enum访问ServiceConfig?
@Component
public class BundleMaker {
@PersistenceContext
private final EntityManager entityManager;
private final Map<Class<? extends IRecord>, Supplier<String>> procNameSupplier = new HashMap<>();
public BundleMaker(EntityManager entityManager, ServiceConfig config) {
this.entityManager = entityManager;
procNameSupplier.put(MainDepRecord.class, config::mainDepProc);
//...
}
public <T extends IRecord> Bundle bundle(Class<T> cl) {
StoredProcedureQuery query;
query = entityManager.createStoredProcedureQuery(procNameSupplier.get(cl).get(), cl)
.registerStoredProcedureParameter(1, cl, REF_CURSOR);
List<T> resultList = query.getResultList();
List<Resource> resources = resultList.stream()
.map(T::toResource)
.collect(Collectors.toList());
return new Bundle(resources);
}
}
I have made this class which creates bundle from database records.
The records are got from stored procedures and names of this procedures are taken from ServiceConfig instance which is Spring @Configuration.
Now I use java Map to bind each DB entity class with according ServiceConfig method.
I would like to extend it and use Enum instead of procNameSupplier here. Is it possible to access ServiceConfig from enum?
@Component
public class BundleMaker {
@PersistenceContext
private final EntityManager entityManager;
private final Map<Class<? extends IRecord>, Supplier<String>> procNameSupplier = new HashMap<>();
public BundleMaker(EntityManager entityManager, ServiceConfig config) {
this.entityManager = entityManager;
procNameSupplier.put(MainDepRecord.class, config::mainDepProc);
//...
}
public <T extends IRecord> Bundle bundle(Class<T> cl) {
StoredProcedureQuery query;
query = entityManager.createStoredProcedureQuery(procNameSupplier.get(cl).get(), cl)
.registerStoredProcedureParameter(1, cl, REF_CURSOR);
List<T> resultList = query.getResultList();
List<Resource> resources = resultList.stream()
.map(T::toResource)
.collect(Collectors.toList());
return new Bundle(resources);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论