[ 
https://issues.apache.org/jira/browse/CAMEL-12087?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16643600#comment-16643600
 ] 

ASF GitHub Bot commented on CAMEL-12087:
----------------------------------------

lburgazzoli closed pull request #2558: CAMEL-12087: camel-core: WARN No 
CamelContext defined yet so cannot inject into bean
URL: https://github.com/apache/camel/pull/2558
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/camel-core/src/main/java/org/apache/camel/DeferredContextBinding.java 
b/camel-core/src/main/java/org/apache/camel/DeferredContextBinding.java
new file mode 100644
index 00000000000..1bf79272a0c
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/DeferredContextBinding.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Used to indicate that if the target type is {@link CamelContextAware}, the
+ * context does not need to be mandatory injected during bean post processing 
but
+ * can be injected later on as example during Camel Context configuration.
+ *
+ * See <a 
href="https://issues.apache.org/jira/browse/CAMEL-12087";>CAMEL-12087</a> for 
additional information.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Target({ElementType.TYPE})
+public @interface DeferredContextBinding {
+}
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
index 10966266521..63cf335b726 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
@@ -22,6 +22,7 @@
 import org.apache.camel.BeanInject;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.DeferredContextBinding;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.PropertyInject;
@@ -64,7 +65,7 @@ public DefaultCamelBeanPostProcessor(CamelContext 
camelContext) {
      * initialization callbacks (like <code>afterPropertiesSet</code>
      * or a custom init-method). The bean will already be populated with 
property values.
      * The returned bean instance may be a wrapper around the original.
-     * 
+     *
      * @param bean the new bean instance
      * @param beanName the name of the bean
      * @return the bean instance to use, either the original or a wrapped one; 
if
@@ -84,10 +85,12 @@ public Object postProcessBeforeInitialization(Object bean, 
String beanName) thro
 
         if (bean instanceof CamelContextAware && canSetCamelContext(bean, 
beanName)) {
             CamelContextAware contextAware = (CamelContextAware)bean;
+            DeferredContextBinding deferredBinding = 
bean.getClass().getAnnotation(DeferredContextBinding.class);
             CamelContext context = getOrLookupCamelContext();
-            if (context == null) {
+
+            if (context == null && deferredBinding == null) {
                 LOG.warn("No CamelContext defined yet so cannot inject into 
bean: {}", beanName);
-            } else {
+            } else if (context != null) {
                 contextAware.setCamelContext(context);
             }
         }
@@ -100,7 +103,7 @@ public Object postProcessBeforeInitialization(Object bean, 
String beanName) thro
      * initialization callbacks (like <code>afterPropertiesSet</code>
      * or a custom init-method). The bean will already be populated with 
property values.
      * The returned bean instance may be a wrapper around the original.
-     * 
+     *
      * @param bean the new bean instance
      * @param beanName the name of the bean
      * @return the bean instance to use, either the original or a wrapped one; 
if
@@ -193,7 +196,7 @@ public void injectField(Field field, String endpointUri, 
String endpointRef, Str
                                Object bean, String beanName) {
         injectField(field, endpointUri, endpointRef, endpointProperty, bean, 
beanName, true);
     }
-    
+
     public void injectField(Field field, String endpointUri, String 
endpointRef, String endpointProperty,
                                Object bean, String beanName, boolean binding) {
         ReflectionHelper.setField(field, bean,
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
 
b/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
index 637a805f9d6..7727b800919 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
@@ -29,11 +29,13 @@
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.DeferredContextBinding;
 import org.apache.camel.Route;
 import org.apache.camel.api.management.mbean.ManagedRouteMBean;
 import org.apache.camel.health.HealthCheck;
 import org.apache.camel.health.HealthCheckRepository;
 
+@DeferredContextBinding
 public class RoutesHealthCheckRepository implements CamelContextAware, 
HealthCheckRepository {
     private final ConcurrentMap<Route, HealthCheck> checks;
     private Set<String> blacklist;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> camel-core: WARN No CamelContext defined yet so cannot inject into bean
> -----------------------------------------------------------------------
>
>                 Key: CAMEL-12087
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12087
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-spring-boot
>    Affects Versions: 2.20.3, 2.21.1
>            Reporter: Luca Burgazzoli
>            Assignee: Luca Burgazzoli
>            Priority: Minor
>             Fix For: 2.21.3
>
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to