Hi,

I made a patch which is based on current trunk guicy-jms example to implements you requirement. Hope it can help you.

Willem

### Eclipse Workspace Patch 1.0
#P camel-example-guice-jms
Index: src/main/java/org/apache/camel/example/guice/jms/SomeBean.java
===================================================================
--- src/main/java/org/apache/camel/example/guice/jms/SomeBean.java (revision 884351) +++ src/main/java/org/apache/camel/example/guice/jms/SomeBean.java (working copy)
@@ -16,13 +16,18 @@
  */
 package org.apache.camel.example.guice.jms;

+import com.google.inject.Inject;
+
 /**
  * @version $Revision$
 */
 public class SomeBean {
+    @Inject
+    private Printer printer;

     public void someMethod(String body) {
-        System.out.println("Received: " + body);
+        printer.print(body);
+        //System.out.println("Received: " + body);
     }

     @Override
Index: src/main/java/org/apache/camel/example/guice/jms/Printer.java
===================================================================
--- src/main/java/org/apache/camel/example/guice/jms/Printer.java (revision 0) +++ src/main/java/org/apache/camel/example/guice/jms/Printer.java (revision 0)
@@ -0,0 +1,25 @@
+/**
+ * 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.example.guice.jms;
+
+public class Printer {
+
+    public void print(String body) {
+        System.out.println("Received: " + body);
+    }
+
+}

Property changes on: src/main/java/org/apache/camel/example/guice/jms/Printer.java
___________________________________________________________________
Added: svn:keywords
   + Rev Date
Added: svn:eol-style
   + native

Index: src/main/java/org/apache/camel/example/guice/jms/MyModule.java
===================================================================
--- src/main/java/org/apache/camel/example/guice/jms/MyModule.java (revision 884351) +++ src/main/java/org/apache/camel/example/guice/jms/MyModule.java (working copy)
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.example.guice.jms;

+import com.google.inject.Injector;
 import com.google.inject.Provides;
 import com.google.inject.name.Named;
 import org.apache.activemq.ActiveMQConnectionFactory;
@@ -37,6 +38,7 @@

         // lets add in any RouteBuilder instances we want to use
         bind(MyRouteBuilder.class);
+        bind(Printer.class);
     }

     /**
@@ -48,4 +50,10 @@
     JmsComponent jms(@Named("activemq.brokerURL") String brokerUrl) {
return JmsComponent.jmsComponent(new ActiveMQConnectionFactory(brokerUrl));
     }
+
+    @Provides
+    @JndiBind("myBean")
+    SomeBean someBean(Injector injector) {
+        return injector.getInstance(SomeBean.class);
+    }
 }
Index: src/main/java/org/apache/camel/example/guice/jms/MyRouteBuilder.java
===================================================================
--- src/main/java/org/apache/camel/example/guice/jms/MyRouteBuilder.java (revision 884351) +++ src/main/java/org/apache/camel/example/guice/jms/MyRouteBuilder.java (working copy)
@@ -45,8 +45,7 @@
                 to("file://target/routeOutput");

         // set up a listener on the file component
-        from("file://target/routeOutput?noop=true").
-                bean(new SomeBean());
+        from("file://target/routeOutput?noop=true").beanRef("myBean");
     }

 }
\ No newline at end of file


mumbly wrote:
So to make things simple, let's base it on the existing Guice/Camel example.
So in SomeBean.java, let's say I want to include an injected component and
add:

public class SomeBean {
  @Inject
  private OtherBean ob;

  public void someMethod(String body) {
     System.out.println("Received: " + body);
     System.out.println("OtherBean: " + ob);
  }

Define OtherBean as you wish. I'd expect(hope) without any other
configuration that I'd get a non-null result for OtherBean, just using
default binding values. It seems that it should be able to autowire (or
whatever the Guicey version of that term is) ob. But though the
configuration seems to be done via Guice, I don't think once "inside the
routing context" that Guice is used to build/instantiate the bean from:

from("file://target/routeOutput?noop=true").
                bean(new SomeBean());

I can see that it seems to have been created by new and not via Guice. What
I'm wondering is if there is some way that I can tell it to grab a via built
up via Guice instead of passing a specific instance.

Does that make sense? As I mentioned, I'm still new with both Guice and
Camel, so I could be way off the mark here.

--Tim



willem.jiang wrote:
What kind of Object you want to inject into the bean component?
Did you add that bind in your module ?

Please let me have a look at your module class.

Willem

mumbly wrote:
I've been taking a look at using Camel with Guice for DI, but I'm having
a
little trouble understanding exactly what the integration provides. I've
got
the configuration aspect and that seems to work fine. But it is unclear
to
me if/how to work with Guice enhanced components. Specifically, if I am
using a bean component which uses @Inject, it doesn't appear that the
bean
is being pulled from the Guice context (in other words, the other
components
aren't being injected).

I'm new to both Camel and Guice, so I could be missing something obvious
here or there may be another pattern for DI using Guice with Camel that I
missed. So I guess my basic question is does the Camel/Guice integration
provide the ability to inject components within the routing chain? with
the
bean component?

I see in the Guice example included in the dist, but it only seems to
deal
with using Guice for configuration of the routes.

--Tim




Reply via email to