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 2019/02/28 13:10:36 UTC

[camel] branch master updated: CAMEL-13243: Spring Main supports Spring IoC for route builder classes.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 54f85ad  CAMEL-13243: Spring Main supports Spring IoC for route builder classes.
54f85ad is described below

commit 54f85ad4d113e10c3026a12407b19ba35ec6e362
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Feb 28 14:10:12 2019 +0100

    CAMEL-13243: Spring Main supports Spring IoC for route builder classes.
---
 .../main/java/org/apache/camel/spring/Main.java    | 24 +++++++++++++++-
 .../java/org/apache/camel/spring/MainTest.java     |  1 -
 .../{MainTest.java => main/MainIoCTest.java}       | 32 +++++++---------------
 .../org/apache/camel/spring/main/MyHelloBean.java  | 27 ++++++++++++++++++
 .../camel/spring/main/MyMainIoCRouteBuilder.java   | 32 ++++++++++++++++++++++
 5 files changed, 92 insertions(+), 24 deletions(-)

diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
index b0a1631..d729aab 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
@@ -22,6 +22,7 @@ import java.io.InputStreamReader;
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.Map;
@@ -30,7 +31,9 @@ import java.util.Set;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
@@ -225,7 +228,26 @@ public class Main extends org.apache.camel.main.MainSupport {
         if (parentContext != null) {
             return new ClassPathXmlApplicationContext(args, parentContext);
         } else {
-            return new ClassPathXmlApplicationContext(args);
+            // okay no application context specified so lets look for either
+            // classpath xml or annotation based
+            if (routeBuilderClasses != null) {
+                AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
+                ac.register(SpringCamelContext.class);
+                Set<String> packages = new LinkedHashSet<>();
+                String[] classes = routeBuilderClasses.split(",");
+                for (String clazz : classes) {
+                    if (clazz.contains(".")) {
+                        String packageName = clazz.substring(0, clazz.lastIndexOf("."));
+                        packages.add(packageName);
+                    }
+                }
+                LOG.info("Using Spring annotation scanning in packages: {}", packages);
+                ac.scan(packages.toArray(new String[packages.size()]));
+                ac.refresh();
+                return ac;
+            } else {
+                return new ClassPathXmlApplicationContext();
+            }
         }
     }
 
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
index 429dbb1..47813f3 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
@@ -54,6 +54,5 @@ public class MainTest extends Assert {
         LOG.debug("Received: " + list);
 
         main.stop();
-
     }
 }
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/main/MainIoCTest.java
similarity index 60%
copy from components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
copy to components/camel-spring/src/test/java/org/apache/camel/spring/main/MainIoCTest.java
index 429dbb1..46c0dd1 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/main/MainIoCTest.java
@@ -14,46 +14,34 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.spring;
-
-import java.util.List;
+package org.apache.camel.spring.main;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.Exchange;
-import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.spring.example.MyProcessor;
+import org.apache.camel.spring.Main;
 import org.junit.Assert;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-public class MainTest extends Assert {
-    private static final Logger LOG = LoggerFactory.getLogger(MainTest.class);
+public class MainIoCTest extends Assert {
 
     @Test
     public void testMain() throws Exception {
         // lets make a simple route
         Main main = new Main();
-        main.addRouteBuilder(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("file://src/test/data?initialDelay=0&delay=10&noop=true").process(new MyProcessor()).to("mock:results");
-            }
-        });
+        // add as class so we get IoC from its packages
+        main.addRouteBuilder(MyMainIoCRouteBuilder.class);
         main.start();
 
         CamelContext camelContext = main.getCamelContext();
 
         MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
-        // in case we add more files in src/test/data
-        endpoint.expectedMinimumMessageCount(2);
-        endpoint.assertIsSatisfied();
-        List<Exchange> list = endpoint.getReceivedExchanges();
+        endpoint.expectedBodiesReceived("I am hello bean");
 
-        LOG.debug("Received: " + list);
+        camelContext.createProducerTemplate().sendBody("direct:start", "Hello World");
 
-        main.stop();
+        endpoint.assertIsSatisfied();
 
+        main.stop();
     }
+
 }
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/main/MyHelloBean.java b/components/camel-spring/src/test/java/org/apache/camel/spring/main/MyHelloBean.java
new file mode 100644
index 0000000..8df7642
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/main/MyHelloBean.java
@@ -0,0 +1,27 @@
+/**
+ * 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.main;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class MyHelloBean {
+
+    public String toString() {
+        return "I am hello bean";
+    }
+}
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/main/MyMainIoCRouteBuilder.java b/components/camel-spring/src/test/java/org/apache/camel/spring/main/MyMainIoCRouteBuilder.java
new file mode 100644
index 0000000..2cb59a9
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/main/MyMainIoCRouteBuilder.java
@@ -0,0 +1,32 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.main;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class MyMainIoCRouteBuilder extends RouteBuilder {
+
+    // use spring IoC annotations
+    @Autowired
+    private MyHelloBean bean;
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:start").transform().constant(bean).to("mock:results");
+    }
+}