I'm running the following code in WildFly to create an EntityManagerFactory
CDI bean for application deployments to use but I don't seem to be able to
use the passed bean name ("pu1") in a test that has:
"
@Inject
@Named("pu1")
EntityManagerFactory entityManagerFactoryByPuName;
"
I expect that the following container code is naming the created bean but I
thought the passed name would work with @Named but it doesn't seem to. The
following EE container code (AfterBeanDiscovery callback) is doing the
following which includes a call to
"beanConfigurator.name(persistenceUnitMetadata.getPersistenceUnitName()):".
Should setting the bean name this way allow @Named references to the passed
name ("pu1" in this case) lookup the created bean as I am showing above?
"
private void entityManagerFactory(
AfterBeanDiscovery afterBeanDiscovery,
PersistenceUnitMetadata persistenceUnitMetadata,
List<String> qualifiers,
IntegrationWithCDIBag integrationWithCDIBag) throws
InstantiationException, IllegalAccessException {
String scope = applicationScoped;
// EntityManagerFactory setup
BeanConfigurator<EntityManagerFactory> beanConfigurator =
afterBeanDiscovery.addBean();
beanConfigurator.addTransitiveTypeClosure(EntityManagerFactory.class);
try {
if (!persistenceUnitMetadata.isDuplicate()) {
beanConfigurator.name(persistenceUnitMetadata.getPersistenceUnitName());
String qualifier = "jakarta.inject.Named";
final Class<? extends Annotation> qualifierType =
persistenceUnitMetadata.getClassLoader()
.loadClass(qualifier)
.asSubclass(Annotation.class);
beanConfigurator.addQualifier(ScopeProxy.createProxy(qualifierType));
}
Class<? extends Annotation> scopeAnnotation =
persistenceUnitMetadata.getClassLoader().loadClass(scope).asSubclass(Annotation.class);
beanConfigurator.scope(scopeAnnotation);
for (String qualifier : qualifiers) {
final Class<? extends Annotation> qualifierType =
persistenceUnitMetadata.getClassLoader()
.loadClass(qualifier)
.asSubclass(Annotation.class);
beanConfigurator.addQualifier(ScopeProxy.createProxy(qualifierType));
}
Class<?> entityManagerFactoryClass = EntityManagerFactory.class;
beanConfigurator.beanClass(entityManagerFactoryClass);
beanConfigurator.produceWith(c -> {
return integrationWithCDIBag.getEntityManagerFactory();
}
);
} catch (ClassNotFoundException e) {
throw JpaLogger.ROOT_LOGGER.classNotFound(e,
persistenceUnitMetadata.getScopedPersistenceUnitName());
}
}
"
Thanks,
Scott