Repository: quarkusio/quarkus
Issue: Hot reload fails when using custom beans validator and MP Rest Client

## Describe the bug
When running in dev mode, hot reload always fails when having a MP Rest Client injected in a custom beans validator. Even if code is changed outside the validator, the reload fails.

### Expected behavior
Changes to code in dev mode should reload without any failure.

### Actual behavior
Getting the following stack trace:

```
java.lang.IllegalStateException: Container not running: ArcContainerImpl [id=1, running=false, beans=0, observers=0, scopes=[]]
	at io.quarkus.arc.impl.ArcContainerImpl.requireRunning(ArcContainerImpl.java:797)
	at io.quarkus.arc.impl.ArcContainerImpl.instanceSupplier(ArcContainerImpl.java:240)
	at io.quarkus.arc.runtime.BeanContainerImpl.instanceFactory(BeanContainerImpl.java:25)
	at io.quarkus.resteasy.common.runtime.QuarkusConstructorInjector.construct(QuarkusConstructorInjector.java:34)
	at org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl.injectedInstance(ResteasyProviderFactoryImpl.java:1399)
	at org.jboss.resteasy.microprofile.client.RestClientBuilderImpl.newInstanceOf(RestClientBuilderImpl.java:477)
	at org.jboss.resteasy.microprofile.client.RestClientBuilderImpl.register(RestClientBuilderImpl.java:489)
	at org.jboss.resteasy.microprofile.client.RestClientBuilderImpl.build(RestClientBuilderImpl.java:190)
	at io.quarkus.restclient.runtime.RestClientBase.create(RestClientBase.java:79)
	at org.acme.AgeLookupService_e9fbd2155c92de4d28b8270959d49ea60024644c_Synthetic_Bean.create(AgeLookupService_e9fbd2155c92de4d28b8270959d49ea60024644c_Synthetic_Bean.zig:189)
	at org.acme.AgeLookupService_e9fbd2155c92de4d28b8270959d49ea60024644c_Synthetic_Bean.get(AgeLookupService_e9fbd2155c92de4d28b8270959d49ea60024644c_Synthetic_Bean.zig:220)
	at org.acme.AgeLookupService_e9fbd2155c92de4d28b8270959d49ea60024644c_Synthetic_Bean.get(AgeLookupService_e9fbd2155c92de4d28b8270959d49ea60024644c_Synthetic_Bean.zig:243)
	at io.quarkus.arc.impl.CurrentInjectionPointProvider.get(CurrentInjectionPointProvider.java:52)
	at org.acme.CustomAgeValidator_Bean.create(CustomAgeValidator_Bean.zig:252)
	at org.acme.CustomAgeValidator_Bean.create(CustomAgeValidator_Bean.zig:292)
	at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:96)
	at io.quarkus.arc.impl.AbstractSharedContext.access$000(AbstractSharedContext.java:14)
	at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:29)
	at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:26)
	at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
	at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
	at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
	at org.acme.CustomAgeValidator_Bean.get(CustomAgeValidator_Bean.zig:324)
	at org.acme.CustomAgeValidator_Bean.get(CustomAgeValidator_Bean.zig:340)
	at io.quarkus.arc.impl.ArcContainerImpl.beanInstanceHandle(ArcContainerImpl.java:430)
	at io.quarkus.arc.impl.ArcContainerImpl.beanInstanceHandle(ArcContainerImpl.java:443)
	at io.quarkus.arc.impl.ArcContainerImpl.instanceHandle(ArcContainerImpl.java:413)
	at io.quarkus.arc.impl.ArcContainerImpl.instance(ArcContainerImpl.java:222)
	at io.quarkus.hibernate.validator.runtime.ArcConstraintValidatorFactoryImpl.getInstance(ArcConstraintValidatorFactoryImpl.java:29)
	at org.hibernate.validator.internal.engine.constraintvalidation.ClassBasedValidatorDescriptor.newInstance(ClassBasedValidatorDescriptor.java:84)
	at org.hibernate.validator.internal.engine.constraintvalidation.AbstractConstraintValidatorManagerImpl.createAndInitializeValidator(AbstractConstraintValidatorManagerImpl.java:89)
	at org.hibernate.validator.internal.engine.constraintvalidation.PredefinedScopeConstraintValidatorManagerImpl.getInitializedValidator(PredefinedScopeConstraintValidatorManagerImpl.java:60)
	at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.<init>(ConstraintTree.java:56)
	at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.<init>(SimpleConstraintTree.java:37)
	at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.of(ConstraintTree.java:66)
	at org.hibernate.validator.internal.metadata.core.MetaConstraint.<init>(MetaConstraint.java:75)
	at org.hibernate.validator.internal.metadata.core.MetaConstraints.create(MetaConstraints.java:74)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getParameterMetaData(AnnotationMetaDataProvider.java:421)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findExecutableMetaData(AnnotationMetaDataProvider.java:308)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMetaData(AnnotationMetaDataProvider.java:292)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMethodMetaData(AnnotationMetaDataProvider.java:279)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:130)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:120)
	at org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.getBeanConfigurationForHierarchy(PredefinedScopeBeanMetaDataManager.java:183)
	at org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.createBeanMetaData(PredefinedScopeBeanMetaDataManager.java:150)
	at org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.<init>(PredefinedScopeBeanMetaDataManager.java:100)
	at org.hibernate.validator.internal.engine.PredefinedScopeValidatorFactoryImpl.<init>(PredefinedScopeValidatorFactoryImpl.java:180)
	at org.hibernate.validator.PredefinedScopeHibernateValidator.buildValidatorFactory(PredefinedScopeHibernateValidator.java:42)
	at org.hibernate.validator.internal.engine.AbstractConfigurationImpl.buildValidatorFactory(AbstractConfigurationImpl.java:433)
	at io.quarkus.hibernate.validator.runtime.HibernateValidatorRecorder$1.created(HibernateValidatorRecorder.java:132)
	at io.quarkus.arc.runtime.ArcRecorder.initBeanContainer(ArcRecorder.java:70)
	at io.quarkus.deployment.steps.ArcProcessor$generateResources-1025303321.deploy_0(ArcProcessor$generateResources-1025303321.zig:138)
	at io.quarkus.deployment.steps.ArcProcessor$generateResources-1025303321.deploy(ArcProcessor$generateResources-1025303321.zig:40)
	at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:259)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at java.base/java.lang.Class.newInstance(Class.java:584)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:65)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:42)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:119)
	at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.runner.bootstrap.StartupActionImpl$3.run(StartupActionImpl.java:134)
	at java.base/java.lang.Thread.run(Thread.java:834)
Resulted in: java.lang.RuntimeException: Error injecting org.acme.AgeLookupService org.acme.CustomAgeValidator.ageLookupService
	at org.acme.CustomAgeValidator_Bean.create(CustomAgeValidator_Bean.zig:269)
	... 55 more
Resulted in: java.lang.RuntimeException: java.lang.RuntimeException: Error injecting org.acme.AgeLookupService org.acme.CustomAgeValidator.ageLookupService
	at io.quarkus.dev.appstate.ApplicationStateNotification.waitForApplicationStart(ApplicationStateNotification.java:51)
	at io.quarkus.runner.bootstrap.StartupActionImpl.runMainClass(StartupActionImpl.java:145)
	at io.quarkus.deployment.dev.IsolatedDevModeMain.restartApp(IsolatedDevModeMain.java:191)
	at io.quarkus.deployment.dev.IsolatedDevModeMain.restartCallback(IsolatedDevModeMain.java:175)
	at io.quarkus.deployment.dev.RuntimeUpdatesProcessor.doScan(RuntimeUpdatesProcessor.java:254)
	at io.quarkus.vertx.http.runtime.devmode.VertxHttpHotReplacementSetup$2.handle(VertxHttpHotReplacementSetup.java:64)
	at io.quarkus.vertx.http.runtime.devmode.VertxHttpHotReplacementSetup$2.handle(VertxHttpHotReplacementSetup.java:54)
	at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$2(ContextImpl.java:313)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	... 1 more
```

## To Reproduce

Steps to reproduce the behavior:
1. clone https://github.com/enbohm/code-with-hotreload
2. start in dev-mode
3. open http://127.0.0.1:8080/validate?age=11
4. make a small change in the code
5. reload http://127.0.0.1:8080/validate?age=11

### Configuration

https://github.com/enbohm/code-with-hotreload/blob/master/src/main/resources/application.properties

## Environment (please complete the following information):

### Output of `uname -a` or `ver`
Darwin MacBook-Pro.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May  8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64

### Output of `java -version`
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

### Quarkus version or git rev
1.13.6

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)
maven 3.8

## Additional context

If adding an @ApplicationScoped annotation on the MP Rest client the reload works


Changed files:
extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusInjectorFactory.java
extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/ResteasyInjectorFactoryRecorder.java
extensions/resteasy-classic/resteasy-common/deployment/src/main/java/io/quarkus/resteasy/common/deployment/ResteasyCommonProcessor.java
extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusConstructorInjector.java

diff --git a/extensions/resteasy-classic/resteasy-common/deployment/src/main/java/io/quarkus/resteasy/common/deployment/ResteasyCommonProcessor.java b/extensions/resteasy-classic/resteasy-common/deployment/src/main/java/io/quarkus/resteasy/common/deployment/ResteasyCommonProcessor.java
index 4d0c9e6baee..bf4654c1a36 100644
--- a/extensions/resteasy-classic/resteasy-common/deployment/src/main/java/io/quarkus/resteasy/common/deployment/ResteasyCommonProcessor.java
+++ b/extensions/resteasy-classic/resteasy-common/deployment/src/main/java/io/quarkus/resteasy/common/deployment/ResteasyCommonProcessor.java
@@ -49,10 +49,10 @@
 import io.quarkus.deployment.Capability;
 import io.quarkus.deployment.annotations.BuildProducer;
 import io.quarkus.deployment.annotations.BuildStep;
+import io.quarkus.deployment.annotations.Consume;
 import io.quarkus.deployment.annotations.Record;
 import io.quarkus.deployment.builditem.AdditionalStaticInitConfigSourceProviderBuildItem;
 import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
-import io.quarkus.deployment.builditem.ProxyUnwrapperBuildItem;
 import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
 import io.quarkus.deployment.util.ServiceUtil;
 import io.quarkus.resteasy.common.runtime.ResteasyInjectorFactoryRecorder;
@@ -148,16 +148,11 @@ void setupGzipProviders(BuildProducer<ResteasyJaxrsProviderBuildItem> providers)
     }
 
     @Record(STATIC_INIT)
+    @Consume(BeanContainerBuildItem.class)
     @BuildStep
-    ResteasyInjectionReadyBuildItem setupResteasyInjection(List<ProxyUnwrapperBuildItem> proxyUnwrappers,
-            BeanContainerBuildItem beanContainerBuildItem,
-            Capabilities capabilities,
+    ResteasyInjectionReadyBuildItem setupResteasyInjection(
             ResteasyInjectorFactoryRecorder recorder) {
-        List<Function<Object, Object>> unwrappers = new ArrayList<>();
-        for (ProxyUnwrapperBuildItem i : proxyUnwrappers) {
-            unwrappers.add(i.getUnwrapper());
-        }
-        RuntimeValue<InjectorFactory> injectorFactory = recorder.setup(beanContainerBuildItem.getValue(), unwrappers);
+        RuntimeValue<InjectorFactory> injectorFactory = recorder.setup();
         return new ResteasyInjectionReadyBuildItem(injectorFactory);
     }
 
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 88723b3295d..218b5e6a3d6 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
@@ -1,6 +1,7 @@
 package io.quarkus.resteasy.common.runtime;
 
 import java.lang.reflect.Constructor;
+import java.util.function.Supplier;
 
 import javax.ws.rs.WebApplicationException;
 
@@ -10,11 +11,12 @@
 import org.jboss.resteasy.spi.HttpRequest;
 import org.jboss.resteasy.spi.HttpResponse;
 
-import io.quarkus.arc.runtime.BeanContainer;
+import io.quarkus.arc.Arc;
+import io.quarkus.arc.InstanceHandle;
 
 public class QuarkusConstructorInjector implements ConstructorInjector {
 
-    private volatile BeanContainer.Factory<?> factory;
+    private volatile Supplier<? extends InstanceHandle<?>> factory;
 
     private final ConstructorInjector delegate;
 
@@ -27,31 +29,27 @@ public QuarkusConstructorInjector(Constructor<?> ctor, ConstructorInjector deleg
 
     @Override
     public Object construct(boolean unwrapAsync) {
-        if (QuarkusInjectorFactory.CONTAINER == null) {
-            return this.delegate.construct(unwrapAsync);
-        }
-        if (factory == null) {
-            factory = QuarkusInjectorFactory.CONTAINER.instanceFactory(this.ctor.getDeclaringClass());
+        if (factory != null) {
+            return factory.get().get();
         }
+        factory = Arc.container().instanceSupplier(this.ctor.getDeclaringClass());
         if (factory == null) {
             return delegate.construct(unwrapAsync);
         }
-        return factory.create().get();
+        return factory.get().get();
     }
 
     @Override
     public Object construct(HttpRequest request, HttpResponse response, boolean unwrapAsync)
             throws Failure, WebApplicationException, ApplicationException {
-        if (QuarkusInjectorFactory.CONTAINER == null) {
-            return delegate.construct(request, response, unwrapAsync);
-        }
-        if (factory == null) {
-            factory = QuarkusInjectorFactory.CONTAINER.instanceFactory(this.ctor.getDeclaringClass());
+        if (factory != null) {
+            return factory.get().get();
         }
+        factory = Arc.container().instanceSupplier(this.ctor.getDeclaringClass());
         if (factory == null) {
             return delegate.construct(request, response, unwrapAsync);
         }
-        return factory.create().get();
+        return factory.get().get();
     }
 
     @Override
diff --git a/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusInjectorFactory.java b/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusInjectorFactory.java
index fb97f717713..cc4bf240826 100644
--- a/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusInjectorFactory.java
+++ b/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/QuarkusInjectorFactory.java
@@ -18,13 +18,12 @@
 import org.jboss.resteasy.spi.metadata.ResourceClass;
 import org.jboss.resteasy.spi.metadata.ResourceConstructor;
 
-import io.quarkus.arc.runtime.BeanContainer;
+import io.quarkus.arc.runtime.ClientProxyUnwrapper;
 
 public class QuarkusInjectorFactory extends InjectorFactoryImpl {
 
     private static final Logger log = Logger.getLogger("io.quarkus.resteasy.runtime");
-    static volatile BeanContainer CONTAINER = null;
-    static volatile Function<Object, Object> PROXY_UNWRAPPER;
+    static final Function<Object, Object> PROXY_UNWRAPPER = new ClientProxyUnwrapper();
 
     @SuppressWarnings("rawtypes")
     @Override
diff --git a/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/ResteasyInjectorFactoryRecorder.java b/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/ResteasyInjectorFactoryRecorder.java
index f3c56f7068d..a8552f9f4ea 100644
--- a/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/ResteasyInjectorFactoryRecorder.java
+++ b/extensions/resteasy-classic/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/ResteasyInjectorFactoryRecorder.java
@@ -1,29 +1,14 @@
 package io.quarkus.resteasy.common.runtime;
 
-import java.util.List;
-import java.util.function.Function;
-
 import org.jboss.resteasy.spi.InjectorFactory;
 
-import io.quarkus.arc.runtime.BeanContainer;
 import io.quarkus.runtime.RuntimeValue;
 import io.quarkus.runtime.annotations.Recorder;
 
 @Recorder
 public class ResteasyInjectorFactoryRecorder {
 
-    public RuntimeValue<InjectorFactory> setup(BeanContainer container, List<Function<Object, Object>> propertyUnwrappers) {
-        QuarkusInjectorFactory.CONTAINER = container;
-        QuarkusInjectorFactory.PROXY_UNWRAPPER = new Function<Object, Object>() {
-            @Override
-            public Object apply(Object o) {
-                Object res = o;
-                for (Function<Object, Object> i : propertyUnwrappers) {
-                    res = i.apply(res);
-                }
-                return res;
-            }
-        };
+    public RuntimeValue<InjectorFactory> setup() {
         return new RuntimeValue<>(new QuarkusInjectorFactory());
     }
 }