HHH-12133 Use the BeanManager-provided ambiguous dependency resolution when possible

This should take care of @Alternative in particular.
This commit is contained in:
Yoann Rodière 2017-12-19 15:52:52 +01:00 committed by Steve Ebersole
parent edc0039afc
commit bc304235a6
2 changed files with 4 additions and 23 deletions

View File

@ -6,10 +6,6 @@
*/
package org.hibernate.resource.beans.internal;
import java.util.Set;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
/**
* @author Steve Ebersole
*/
@ -22,24 +18,6 @@ public class Helper {
private Helper() {
}
@SuppressWarnings("unchecked")
public <T> Bean<T> getNamedBean(String beanName, Class<T> beanContract, BeanManager beanManager) {
Set<Bean<?>> beans = beanManager.getBeans( beanContract, new NamedBeanQualifier( beanName ) );
if ( beans.isEmpty() ) {
throw new IllegalArgumentException(
"BeanManager returned no matching beans: name = " + beanName + "; contract = " + beanContract.getName()
);
}
if ( beans.size() > 1 ) {
throw new IllegalArgumentException(
"BeanManager returned multiple matching beans: name = " + beanName + "; contract = " + beanContract.getName()
);
}
return (Bean<T>) beans.iterator().next();
}
public CdiLifecycleManagementStrategy getLifecycleManagementStrategy(boolean shouldRegistryManageLifecycle) {
if ( shouldRegistryManageLifecycle ) {
return JpaCdiLifecycleManagementStrategy.INSTANCE;

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.resource.beans.internal;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
@ -48,9 +49,11 @@ class JpaCdiLifecycleManagementStrategy implements CdiLifecycleManagementStrateg
return new JpaManagedBeanImpl<>( beanClass, injectionTarget, creationalContext, beanInstance );
}
@SuppressWarnings("unchecked")
@Override
public <T> ManagedBean<T> createBean(BeanManager beanManager, String beanName, Class<T> beanClass) {
Bean<T> bean = Helper.INSTANCE.getNamedBean( beanName, beanClass, beanManager );
Set<Bean<?>> beans = beanManager.getBeans( beanClass, new NamedBeanQualifier( beanName ) );
Bean<T> bean = (Bean<T>) beanManager.resolve( beans );
CreationalContext<T> creationalContext = beanManager.createCreationalContext( null );