diff --git a/extensions/amazon-lambda/runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AmazonLambdaRecorder.java b/extensions/amazon-lambda/runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AmazonLambdaRecorder.java index dbec82f28d10d..5d8ce207a88e1 100644 --- a/extensions/amazon-lambda/runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AmazonLambdaRecorder.java +++ b/extensions/amazon-lambda/runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AmazonLambdaRecorder.java @@ -77,11 +77,11 @@ public void setHandlerClass(Class> handler, BeanC */ public static void handle(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { if (streamHandlerClass != null) { - RequestStreamHandler handler = beanContainer.instance(streamHandlerClass); + RequestStreamHandler handler = beanContainer.beanInstance(streamHandlerClass); handler.handleRequest(inputStream, outputStream, context); } else { Object request = objectReader.readValue(inputStream); - RequestHandler handler = beanContainer.instance(handlerClass); + RequestHandler handler = beanContainer.beanInstance(handlerClass); Object response = handler.handleRequest(request, context); objectWriter.writeValue(outputStream, response); } @@ -166,7 +166,7 @@ public void startPollLoop(ShutdownContext context, LaunchMode launchMode) { @Override protected Object processRequest(Object input, AmazonLambdaContext context) throws Exception { - RequestHandler handler = beanContainer.instance(handlerClass); + RequestHandler handler = beanContainer.beanInstance(handlerClass); return handler.handleRequest(input, context); } @@ -188,7 +188,7 @@ protected boolean isStream() { @Override protected void processRequest(InputStream input, OutputStream output, AmazonLambdaContext context) throws Exception { - RequestStreamHandler handler = beanContainer.instance(streamHandlerClass); + RequestStreamHandler handler = beanContainer.beanInstance(streamHandlerClass); handler.handleRequest(input, output, context); } diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/unused/UnusedExclusionTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/unused/UnusedExclusionTest.java index 3f81e738c8f6a..03a84facdf2dd 100644 --- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/unused/UnusedExclusionTest.java +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/unused/UnusedExclusionTest.java @@ -74,7 +74,7 @@ public static class TestRecorder { public void test(BeanContainer beanContainer) { // This should trigger the warning - Gama was removed - Gama gama = beanContainer.instance(Gama.class); + Gama gama = beanContainer.beanInstance(Gama.class); // Test that fallback was used - no injection was performed Assertions.assertNull(gama.beanManager); } diff --git a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainer.java b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainer.java index df187a9e0c94c..77dfa066f4e78 100644 --- a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainer.java +++ b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainer.java @@ -10,11 +10,33 @@ public interface BeanContainer { /** + * Returns a bean instance for given bean type and qualifiers. + *

+ * This method follows standard CDI rules meaning that if there are two or more eligible beans, an ambiguous + * dependency exception is thrown. + * Note that the method is allowed to return {@code null} if there is no matching bean which allows + * for fallback implementations. * * @param type * @param qualifiers * @return a bean instance or {@code null} if no matching bean is found */ + default T beanInstance(Class type, Annotation... qualifiers) { + return beanInstanceFactory(type, qualifiers).create().get(); + } + + /** + * This method is deprecated and will be removed in future versions. + * Use {@link #beanInstance(Class, Annotation...)} instead. + *

+ * As opposed to {@link #beanInstance(Class, Annotation...)}, this method does NOT follow CDI + * resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter. + * + * @param type + * @param qualifiers + * @return a bean instance or {@code null} if no matching bean is found + */ + @Deprecated default T instance(Class type, Annotation... qualifiers) { return instanceFactory(type, qualifiers).create().get(); } @@ -31,6 +53,20 @@ default T instance(Class type, Annotation... qualifiers) { * @param qualifiers * @return a bean instance factory, never {@code null} */ + Factory beanInstanceFactory(Class type, Annotation... qualifiers); + + /** + * This method is deprecated and will be removed in future versions. + * Use {@link #beanInstanceFactory(Class, Annotation...)} instead. + *

+ * As opposed to {@link #beanInstanceFactory(Class, Annotation...)}, this method does NOT follow CDI + * resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter. + * + * @param type + * @param qualifiers + * @return a bean instance factory, never {@code null} + */ + @Deprecated Factory instanceFactory(Class type, Annotation... qualifiers); /** diff --git a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java index a2b2d46495c41..de4a28e5c11df 100644 --- a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java +++ b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java @@ -21,9 +21,19 @@ public BeanContainerImpl(ArcContainer container) { this.container = container; } + @Override + public Factory beanInstanceFactory(Class type, Annotation... qualifiers) { + Supplier> handleSupplier = container.beanInstanceSupplier(type, qualifiers); + return createFactory(handleSupplier, type, qualifiers); + } + @Override public Factory instanceFactory(Class type, Annotation... qualifiers) { Supplier> handleSupplier = container.instanceSupplier(type, qualifiers); + return createFactory(handleSupplier, type, qualifiers); + } + + private Factory createFactory(Supplier> handleSupplier, Class type, Annotation... qualifiers) { if (handleSupplier == null) { LOGGER.debugf( "No matching bean found for type %s and qualifiers %s. The bean might have been marked as unused and removed during build.", diff --git a/extensions/elytron-security/runtime/src/main/java/io/quarkus/elytron/security/runtime/ElytronRecorder.java b/extensions/elytron-security/runtime/src/main/java/io/quarkus/elytron/security/runtime/ElytronRecorder.java index 1285e779a7486..a2ebff71bc80e 100644 --- a/extensions/elytron-security/runtime/src/main/java/io/quarkus/elytron/security/runtime/ElytronRecorder.java +++ b/extensions/elytron-security/runtime/src/main/java/io/quarkus/elytron/security/runtime/ElytronRecorder.java @@ -31,7 +31,7 @@ public void runLoadTask(Runnable runnable) { } public void setDomainForIdentityProvider(BeanContainer bc, RuntimeValue domain) { - bc.instance(ElytronSecurityDomainManager.class).setDomain(domain.getValue()); + bc.beanInstance(ElytronSecurityDomainManager.class).setDomain(domain.getValue()); } /** diff --git a/extensions/funqy/funqy-google-cloud-functions/runtime/src/main/java/io/quarkus/funqy/gcp/functions/FunqyCloudFunctionsBindingRecorder.java b/extensions/funqy/funqy-google-cloud-functions/runtime/src/main/java/io/quarkus/funqy/gcp/functions/FunqyCloudFunctionsBindingRecorder.java index e643ac84819e4..4ae45066d22e2 100644 --- a/extensions/funqy/funqy-google-cloud-functions/runtime/src/main/java/io/quarkus/funqy/gcp/functions/FunqyCloudFunctionsBindingRecorder.java +++ b/extensions/funqy/funqy-google-cloud-functions/runtime/src/main/java/io/quarkus/funqy/gcp/functions/FunqyCloudFunctionsBindingRecorder.java @@ -32,7 +32,7 @@ public class FunqyCloudFunctionsBindingRecorder { public void init(BeanContainer bc) { beanContainer = bc; - objectMapper = beanContainer.instance(ObjectMapper.class); + objectMapper = beanContainer.beanInstance(ObjectMapper.class); for (FunctionInvoker invoker : FunctionRecorder.registry.invokers()) { if (invoker.hasInput()) { diff --git a/extensions/funqy/funqy-server-common/runtime/src/main/java/io/quarkus/funqy/runtime/FunctionConstructor.java b/extensions/funqy/funqy-server-common/runtime/src/main/java/io/quarkus/funqy/runtime/FunctionConstructor.java index d6f2f25386c6e..56abc399ebcb6 100644 --- a/extensions/funqy/funqy-server-common/runtime/src/main/java/io/quarkus/funqy/runtime/FunctionConstructor.java +++ b/extensions/funqy/funqy-server-common/runtime/src/main/java/io/quarkus/funqy/runtime/FunctionConstructor.java @@ -14,7 +14,7 @@ public FunctionConstructor(Class cls) { public T construct() { if (factory == null) - factory = CONTAINER.instanceFactory(cls); + factory = CONTAINER.beanInstanceFactory(cls); return factory.create().get(); } } diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRecorder.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRecorder.java index f5c22b8521543..31a74d5dabfad 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRecorder.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRecorder.java @@ -92,7 +92,7 @@ public DataSourceTenantConnectionResolver get() { } public void startAllPersistenceUnits(BeanContainer beanContainer) { - beanContainer.instance(JPAConfig.class).startAll(); + beanContainer.beanInstance(JPAConfig.class).startAll(); } public Supplier sessionFactorySupplier(String persistenceUnitName) { diff --git a/extensions/infinispan-client/runtime/src/main/java/io/quarkus/infinispan/client/runtime/InfinispanRecorder.java b/extensions/infinispan-client/runtime/src/main/java/io/quarkus/infinispan/client/runtime/InfinispanRecorder.java index 49f4466156a26..5534f9658d78c 100644 --- a/extensions/infinispan-client/runtime/src/main/java/io/quarkus/infinispan/client/runtime/InfinispanRecorder.java +++ b/extensions/infinispan-client/runtime/src/main/java/io/quarkus/infinispan/client/runtime/InfinispanRecorder.java @@ -11,7 +11,7 @@ public class InfinispanRecorder { public BeanContainerListener configureInfinispan(@RelaxedValidation Properties properties) { return container -> { - InfinispanClientProducer instance = container.instance(InfinispanClientProducer.class); + InfinispanClientProducer instance = container.beanInstance(InfinispanClientProducer.class); instance.configure(properties); }; } diff --git a/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusConstructorInjector.java b/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusConstructorInjector.java index 218b5e6a3d6ef..df4ac2bf252a9 100644 --- a/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusConstructorInjector.java +++ b/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusConstructorInjector.java @@ -32,7 +32,7 @@ public Object construct(boolean unwrapAsync) { if (factory != null) { return factory.get().get(); } - factory = Arc.container().instanceSupplier(this.ctor.getDeclaringClass()); + factory = Arc.container().beanInstanceSupplier(this.ctor.getDeclaringClass()); if (factory == null) { return delegate.construct(unwrapAsync); } @@ -45,7 +45,7 @@ public Object construct(HttpRequest request, HttpResponse response, boolean unwr if (factory != null) { return factory.get().get(); } - factory = Arc.container().instanceSupplier(this.ctor.getDeclaringClass()); + factory = Arc.container().beanInstanceSupplier(this.ctor.getDeclaringClass()); if (factory == null) { return delegate.construct(request, response, unwrapAsync); } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-common/runtime/src/main/java/io/quarkus/resteasy/reactive/common/runtime/ArcBeanFactory.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-common/runtime/src/main/java/io/quarkus/resteasy/reactive/common/runtime/ArcBeanFactory.java index c44b8bb45458c..25656077526e0 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-common/runtime/src/main/java/io/quarkus/resteasy/reactive/common/runtime/ArcBeanFactory.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-common/runtime/src/main/java/io/quarkus/resteasy/reactive/common/runtime/ArcBeanFactory.java @@ -12,7 +12,7 @@ public class ArcBeanFactory implements BeanFactory { public ArcBeanFactory(Class target, BeanContainer beanContainer) { targetClassName = target.getName(); - factory = beanContainer.instanceFactory(target); + factory = beanContainer.beanInstanceFactory(target); } @Override diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/ResteasyReactiveRecorder.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/ResteasyReactiveRecorder.java index bff2c062d5962..b884ee5a741b8 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/ResteasyReactiveRecorder.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/ResteasyReactiveRecorder.java @@ -133,7 +133,7 @@ public RuntimeValue createDeployment(DeploymentInfo info, } CurrentRequestManager - .setCurrentRequestInstance(new QuarkusCurrentRequest(beanContainer.instance(CurrentVertxRequest.class))); + .setCurrentRequestInstance(new QuarkusCurrentRequest(beanContainer.beanInstance(CurrentVertxRequest.class))); BlockingOperationSupport.setIoThreadDetector(new BlockingOperationSupport.IOThreadDetector() { @Override diff --git a/extensions/security-webauthn/runtime/src/main/java/io/quarkus/security/webauthn/WebAuthnRecorder.java b/extensions/security-webauthn/runtime/src/main/java/io/quarkus/security/webauthn/WebAuthnRecorder.java index ff44d478c63c8..72fe655cb61ba 100644 --- a/extensions/security-webauthn/runtime/src/main/java/io/quarkus/security/webauthn/WebAuthnRecorder.java +++ b/extensions/security-webauthn/runtime/src/main/java/io/quarkus/security/webauthn/WebAuthnRecorder.java @@ -32,9 +32,9 @@ public WebAuthnRecorder(RuntimeValue httpConfiguration, Runti } public void setupRoutes(BeanContainer beanContainer, RuntimeValue routerValue, String prefix) { - WebAuthnSecurity security = beanContainer.instance(WebAuthnSecurity.class); - WebAuthnAuthenticationMechanism authMech = beanContainer.instance(WebAuthnAuthenticationMechanism.class); - IdentityProviderManager identityProviderManager = beanContainer.instance(IdentityProviderManager.class); + WebAuthnSecurity security = beanContainer.beanInstance(WebAuthnSecurity.class); + WebAuthnAuthenticationMechanism authMech = beanContainer.beanInstance(WebAuthnAuthenticationMechanism.class); + IdentityProviderManager identityProviderManager = beanContainer.beanInstance(IdentityProviderManager.class); WebAuthnController controller = new WebAuthnController(security, config.getValue(), identityProviderManager, authMech); Router router = routerValue.getValue(); BodyHandler bodyHandler = BodyHandler.create(); diff --git a/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLRecorder.java b/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLRecorder.java index 8e634b0de9892..048ac5e65d622 100644 --- a/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLRecorder.java +++ b/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLRecorder.java @@ -26,7 +26,7 @@ public class SmallRyeGraphQLRecorder { public RuntimeValue createExecutionService(BeanContainer beanContainer, Schema schema) { - GraphQLProducer graphQLProducer = beanContainer.instance(GraphQLProducer.class); + GraphQLProducer graphQLProducer = beanContainer.beanInstance(GraphQLProducer.class); GraphQLSchema graphQLSchema = graphQLProducer.initialize(schema); return new RuntimeValue<>(graphQLSchema != null); } diff --git a/extensions/smallrye-metrics/runtime/src/main/java/io/quarkus/smallrye/metrics/runtime/SmallRyeMetricsRecorder.java b/extensions/smallrye-metrics/runtime/src/main/java/io/quarkus/smallrye/metrics/runtime/SmallRyeMetricsRecorder.java index f087fc6124305..08966f4712840 100644 --- a/extensions/smallrye-metrics/runtime/src/main/java/io/quarkus/smallrye/metrics/runtime/SmallRyeMetricsRecorder.java +++ b/extensions/smallrye-metrics/runtime/src/main/java/io/quarkus/smallrye/metrics/runtime/SmallRyeMetricsRecorder.java @@ -194,7 +194,7 @@ public void createRegistries(BeanContainer container) { //HACK: registration is done via statics, but cleanup is done via pre destroy //however if the bean is not used it will not be created, so no cleanup will be done //we force bean creation here to make sure the container can restart correctly - container.instance(MetricRegistries.class).getApplicationRegistry(); + container.beanInstance(MetricRegistries.class).getApplicationRegistry(); } public void dropRegistriesAtShutdown(ShutdownContext shutdownContext) { diff --git a/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java b/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java index bcb9095df2ac4..0975c2cce33f4 100644 --- a/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java +++ b/extensions/undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowDeploymentRecorder.java @@ -252,7 +252,7 @@ public RuntimeValue registerServlet(RuntimeValue de InstanceFactory instanceFactory) throws Exception { InstanceFactory factory = instanceFactory != null ? instanceFactory - : new QuarkusInstanceFactory(beanContainer.instanceFactory(servletClass)); + : new QuarkusInstanceFactory(beanContainer.beanInstanceFactory(servletClass)); ServletInfo servletInfo = new ServletInfo(name, (Class) servletClass, factory); deploymentInfo.getValue().addServlet(servletInfo); @@ -304,7 +304,7 @@ public RuntimeValue registerFilter(RuntimeValue info InstanceFactory instanceFactory) throws Exception { InstanceFactory factory = instanceFactory != null ? instanceFactory - : new QuarkusInstanceFactory(beanContainer.instanceFactory(filterClass)); + : new QuarkusInstanceFactory(beanContainer.beanInstanceFactory(filterClass)); FilterInfo filterInfo = new FilterInfo(name, (Class) filterClass, factory); info.getValue().addFilter(filterInfo); filterInfo.setAsyncSupported(asyncSupported); @@ -329,7 +329,7 @@ public void registerListener(RuntimeValue info, Class listene info.getValue() .addListener(new ListenerInfo((Class) listenerClass, (InstanceFactory) new QuarkusInstanceFactory<>( - factory.instanceFactory(listenerClass)))); + factory.beanInstanceFactory(listenerClass)))); } public void addMimeMapping(RuntimeValue info, String extension, @@ -457,7 +457,7 @@ public DeploymentManager bootServletContainer(RuntimeValue info, info.getValue().setClassIntrospecter(new ClassIntrospecter() { @Override public InstanceFactory createInstanceFactory(Class clazz) throws NoSuchMethodException { - BeanContainer.Factory res = beanContainer.instanceFactory(clazz); + BeanContainer.Factory res = beanContainer.beanInstanceFactory(clazz); if (res == null) { return defaultVal.createInstanceFactory(clazz); } diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java index f4ca987fb85a2..053aa4542fcd9 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java @@ -241,7 +241,7 @@ public BeanContainerListener initPermissions(HttpBuildTimeConfig permissions, return new BeanContainerListener() { @Override public void created(BeanContainer container) { - container.instance(PathMatchingHttpSecurityPolicy.class).init(permissions, policies); + container.beanInstance(PathMatchingHttpSecurityPolicy.class).init(permissions, policies); } }; } diff --git a/extensions/websockets/client/runtime/src/main/java/io/quarkus/websockets/client/runtime/WebsocketCoreRecorder.java b/extensions/websockets/client/runtime/src/main/java/io/quarkus/websockets/client/runtime/WebsocketCoreRecorder.java index 93ed683e37a61..ca321461c596d 100644 --- a/extensions/websockets/client/runtime/src/main/java/io/quarkus/websockets/client/runtime/WebsocketCoreRecorder.java +++ b/extensions/websockets/client/runtime/src/main/java/io/quarkus/websockets/client/runtime/WebsocketCoreRecorder.java @@ -125,7 +125,7 @@ public RuntimeValue createServerContainer(BeanContaine ServerWebSocketContainer container = serverContainerFactory.create(new ObjectIntrospecter() { @Override public ObjectFactory createInstanceFactory(Class clazz) { - BeanContainer.Factory factory = beanContainer.instanceFactory(clazz); + BeanContainer.Factory factory = beanContainer.beanInstanceFactory(clazz); return new ObjectFactory() { @Override public ObjectHandle createInstance() { diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java index 6f778ca19e69a..19f49a9b2de57 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java @@ -99,6 +99,21 @@ public interface ArcContainer { * @param * @return */ + Supplier> beanInstanceSupplier(Class type, Annotation... qualifiers); + + /** + * This method is deprecated and will be removed in future versions. + * Use {@link #beanInstanceSupplier(Class, Annotation...)} instead. + *

+ * As opposed to {@link #beanInstanceSupplier(Class, Annotation...)}, this method does NOT follow CDI + * resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter. + * + * @param type + * @param qualifiers + * @return + * @param + */ + @Deprecated Supplier> instanceSupplier(Class type, Annotation... qualifiers); /** diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java index 94230917eb52c..3c88386ab5925 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java @@ -235,17 +235,41 @@ public InstanceHandle instance(Type type, Annotation... qualifiers) { } @SuppressWarnings("unchecked") + @Override + public Supplier> beanInstanceSupplier(Class type, Annotation... qualifiers) { + return createInstanceSupplier(false, type, qualifiers); + } + @Override public Supplier> instanceSupplier(Class type, Annotation... qualifiers) { + return createInstanceSupplier(true, type, qualifiers); + } + + private Supplier> createInstanceSupplier(boolean resolveAmbiguities, Class type, + Annotation... qualifiers) { if (qualifiers == null || qualifiers.length == 0) { qualifiers = new Annotation[] { Default.Literal.INSTANCE }; } Set> resolvedBeans = resolved.getValue(new Resolvable(type, qualifiers)); + Set> filteredBean = resolvedBeans; if (resolvedBeans.size() > 1) { - throw new AmbiguousResolutionException("Beans: " + resolvedBeans); + if (resolveAmbiguities) { + // this is non-standard CDI behavior that we momentarily keep to retain compatibility + // if there are multiple beans we look for an exact match + // this method is only called with the exact type required + // so ignoring subclasses is the correct behaviour + filteredBean = new HashSet<>(); + for (InjectableBean i : resolvedBeans) { + if (i.getBeanClass().equals(type)) { + filteredBean.add(i); + } + } + } else { + throw new AmbiguousResolutionException("Beans: " + resolvedBeans); + } } - InjectableBean bean = resolvedBeans.size() != 1 ? null - : (InjectableBean) resolvedBeans.iterator().next(); + InjectableBean bean = filteredBean.size() != 1 ? null + : (InjectableBean) filteredBean.iterator().next(); if (bean == null) { return null; } diff --git a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/TestRecorder.java b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/TestRecorder.java index d7f90b7c5eb84..ae6cf57794b9a 100644 --- a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/TestRecorder.java +++ b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/TestRecorder.java @@ -54,7 +54,7 @@ public void startRuntimeService(ShutdownContext shutdownContext, RuntimeValue