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 2024/01/25 08:19:41 UTC

(camel-spring-boot) branch main updated: CAMEL-20352: camel-spring-boot - camel.springboot.includeNonSingletons is not respected anymore: prototype Route Builders are always initialized.

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-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 41e7d160418 CAMEL-20352: camel-spring-boot - camel.springboot.includeNonSingletons is not respected anymore: prototype Route Builders are always initialized.
41e7d160418 is described below

commit 41e7d1604183705135abdf31d2a6c56390b397ad
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jan 25 09:19:28 2024 +0100

    CAMEL-20352: camel-spring-boot - camel.springboot.includeNonSingletons is not respected anymore: prototype Route Builders are always initialized.
---
 .../camel/spring/boot/CamelAutoConfiguration.java  |  5 +--
 .../boot/CamelSpringBootRoutesCollector.java       | 52 ++++++++++++++++++++++
 .../endpointdsl/EndpointDslAutoConfiguration.java  |  6 ++-
 .../endpointdsl/EndpointDslRouteCollector.java     |  9 +++-
 4 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index f49794f995a..06e904e4073 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -257,9 +257,8 @@ public class CamelAutoConfiguration {
     @Bean
     @ConditionalOnMissingBean(RoutesCollector.class)
     @ConditionalOnMissingClass("org.apache.camel.spring.boot.endpointdsl.EndpointDslRouteCollector")
-    RoutesCollector routesCollector() {
-        // default collector works with spring boot
-        return new DefaultRoutesCollector();
+    RoutesCollector routesCollector(ApplicationContext applicationContext, CamelConfigurationProperties config) {
+        return new CamelSpringBootRoutesCollector(applicationContext, config.isIncludeNonSingletons());
     }
 
     @Bean
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootRoutesCollector.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootRoutesCollector.java
new file mode 100644
index 00000000000..bad80472ea9
--- /dev/null
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootRoutesCollector.java
@@ -0,0 +1,52 @@
+/*
+ * 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.spring.boot;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.main.DefaultRoutesCollector;
+import org.apache.camel.support.DefaultRegistry;
+import org.springframework.context.ApplicationContext;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+public class CamelSpringBootRoutesCollector extends DefaultRoutesCollector {
+
+    private final ApplicationContext applicationContext;
+    private final boolean includeNonSingletons;
+
+    public CamelSpringBootRoutesCollector(ApplicationContext applicationContext, boolean includeNonSingletons) {
+        this.applicationContext = applicationContext;
+        this.includeNonSingletons = includeNonSingletons;
+    }
+
+    @Override
+    protected <T> Collection<T> findByType(CamelContext camelContext, Class<T> type) {
+        // only spring-boot knows about non-singleton beans
+        Map<String, T> beans = applicationContext.getBeansOfType(type, includeNonSingletons, true);
+        Collection<T> answer = new ArrayList<>(beans.values());
+        // there may be additional routes from fallback registry
+        DefaultRegistry dr = camelContext.getRegistry(DefaultRegistry.class);
+        if (dr != null) {
+            Set<T> extra = dr.getFallbackRegistry().findByType(type);
+            answer.addAll(extra);
+        }
+        return answer;
+    }
+}
diff --git a/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslAutoConfiguration.java b/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslAutoConfiguration.java
index f67d07c4212..775a1faa493 100644
--- a/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslAutoConfiguration.java
+++ b/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslAutoConfiguration.java
@@ -17,7 +17,9 @@
 package org.apache.camel.spring.boot.endpointdsl;
 
 import org.apache.camel.main.RoutesCollector;
+import org.apache.camel.spring.boot.CamelConfigurationProperties;
 import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Role;
@@ -30,8 +32,8 @@ public class EndpointDslAutoConfiguration {
      * Special routes collector when using Endpoint DSL.
      */
     @Bean
-    RoutesCollector endpointDslRoutesCollector() {
-        return new EndpointDslRouteCollector();
+    RoutesCollector endpointDslRoutesCollector(ApplicationContext applicationContext, CamelConfigurationProperties config) {
+        return new EndpointDslRouteCollector(applicationContext, config.isIncludeNonSingletons());
     }
 
 }
diff --git a/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslRouteCollector.java b/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslRouteCollector.java
index 9b25d3cd7c7..50d236cdfa9 100644
--- a/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslRouteCollector.java
+++ b/dsl-starter/camel-endpointdsl-starter/src/main/java/org/apache/camel/spring/boot/endpointdsl/EndpointDslRouteCollector.java
@@ -24,13 +24,18 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
 import org.apache.camel.builder.endpoint.LambdaEndpointRouteBuilder;
-import org.apache.camel.main.DefaultRoutesCollector;
+import org.apache.camel.spring.boot.CamelSpringBootRoutesCollector;
+import org.springframework.context.ApplicationContext;
 
 /**
  * Enhanced {@link org.apache.camel.main.RoutesCollector} that supports Endpoint DSL with the
  * lambda style {@link LambdaEndpointRouteBuilder}.
  */
-public class EndpointDslRouteCollector extends DefaultRoutesCollector {
+public class EndpointDslRouteCollector extends CamelSpringBootRoutesCollector {
+
+    public EndpointDslRouteCollector(ApplicationContext applicationContext, boolean includeNonSingletons) {
+        super(applicationContext, includeNonSingletons);
+    }
 
     @Override
     protected Collection<RoutesBuilder> collectAdditionalRoutesFromRegistry(CamelContext camelContext, String excludePattern, String includePattern) {