You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/08 15:18:10 UTC

(camel) branch inject-ds created (now 0d76ae3e34c)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch inject-ds
in repository https://gitbox.apache.org/repos/asf/camel.git


      at 0d76ae3e34c CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager.

This branch includes the following new commits:

     new 0d76ae3e34c CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch inject-ds
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0d76ae3e34c6efc612bbb4dcf42603955783662d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Dec 8 16:17:56 2023 +0100

    CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager.
---
 .../impl/engine/DefaultCamelBeanPostProcessor.java |   5 +-
 ...amelBeanPostProcessorComplexFieldFirstTest.java | 130 +++++++++++++++++++++
 2 files changed, 133 insertions(+), 2 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
index 302230f5090..69406c74a12 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
@@ -224,7 +224,9 @@ public class DefaultCamelBeanPostProcessor implements CamelBeanPostProcessor, Ca
     }
 
     protected void injectSecondPass(Object bean, String beanName, Function<Class<?>, Boolean> filter) {
-        // on second pass do bind to registry beforehand as they may be used by field/method injections below
+        // on second pass do bind to fields first
+        injectFields(bean, beanName, filter);
+
         if (bindToRegistrySupported()) {
             injectClass(bean, beanName);
             injectNestedClasses(bean, beanName);
@@ -232,7 +234,6 @@ public class DefaultCamelBeanPostProcessor implements CamelBeanPostProcessor, Ca
             injectBindToRegistryMethods(bean, beanName, filter);
         }
 
-        injectFields(bean, beanName, filter);
         injectMethods(bean, beanName, filter);
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorComplexFieldFirstTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorComplexFieldFirstTest.java
new file mode 100644
index 00000000000..c886d5aaed1
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorComplexFieldFirstTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.impl;
+
+import org.apache.camel.BeanInject;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.apache.camel.support.PluginHelper;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import javax.sql.DataSource;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.logging.Logger;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class DefaultCamelBeanPostProcessorComplexFieldFirstTest extends ContextTestSupport {
+
+    private CamelBeanPostProcessor postProcessor;
+
+    @Test
+    public void testPostProcessor() throws Exception {
+        FooService foo = new FooService();
+
+        context.getRegistry().bind("myDS", new DummyDataSource());
+
+        postProcessor.postProcessBeforeInitialization(foo, "foo");
+        postProcessor.postProcessAfterInitialization(foo, "foo");
+
+        // should register the beans in the registry via @BindRegistry
+        Object bean = context.getRegistry().lookupByName("myCoolBean");
+        assertNotNull(bean);
+        MySerialBean msb = assertIsInstanceOf(MySerialBean.class, bean);
+
+        assertEquals(123, msb.getId());
+        assertEquals(DummyDataSource.class.getName(), msb.getName());
+    }
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        super.setUp();
+        postProcessor = PluginHelper.getBeanPostProcessor(context);
+    }
+
+    @BindToRegistry
+    public static class FooService {
+
+        @BeanInject
+        private DataSource ctx;
+
+        @BindToRegistry("myCoolBean")
+        public MySerialBean myBean() {
+            MySerialBean myBean = new MySerialBean();
+            myBean.setId(123);
+            myBean.setName(ctx.getClass().getName());
+            return myBean;
+        }
+
+    }
+
+    private class DummyDataSource implements DataSource {
+
+        @Override
+        public Connection getConnection() throws SQLException {
+            return null;
+        }
+
+        @Override
+        public Connection getConnection(String username, String password) throws SQLException {
+            return null;
+        }
+
+        @Override
+        public PrintWriter getLogWriter() throws SQLException {
+            return null;
+        }
+
+        @Override
+        public void setLogWriter(PrintWriter out) throws SQLException {
+
+        }
+
+        @Override
+        public void setLoginTimeout(int seconds) throws SQLException {
+
+        }
+
+        @Override
+        public int getLoginTimeout() throws SQLException {
+            return 0;
+        }
+
+        @Override
+        public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+            return null;
+        }
+
+        @Override
+        public <T> T unwrap(Class<T> iface) throws SQLException {
+            return null;
+        }
+
+        @Override
+        public boolean isWrapperFor(Class<?> iface) throws SQLException {
+            return false;
+        }
+    }
+}