Can I access to Springs components from enum?

Viewed 44

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);
    }
}
0 Answers
Related