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:35:12 UTC
(camel) branch main updated: CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager. (#12381)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new d3254a43a52 CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager. (#12381)
d3254a43a52 is described below
commit d3254a43a5243faa4e654d0ddb6e55d23d41bbeb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Dec 8 16:35:07 2023 +0100
CAMEL-20213: camel-core dependency injection should inject fields with @BeanInject more eager. (#12381)
---
.../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;
+ }
+ }
+}