Guice is a dependency injection (DI) framework that can be used to configure applications. DI was made popular by Spring, and Guice provides nice Jave-based (rather than XML-based) application wiring. From the Guice front page -
"Put simply, Guice alleviates the need for factories and the use of new in your Java code. Think of Guice's @Inject as the new new. You will still need to write factories in some cases, but your code will not depend directly on them. Your code will be easier to change, unit test and reuse in other contexts."
iBeans offers Guice as an option for wiring together your applications. Guice has the concept of a Module, a java class that defines what objects are created and how they are bound to each other. For example, some iBeans channels such as JMS need to have a channel configured with a connection factory -
public MyChannelsConfigModule extends AbstractGuiceIBeansModule { protected void doConfigure() throws Exception { //Create the MuleMQ connector Connector connector = new MuleMQConnector(); //We use this in the Receive annotation on the JmsBean object bind(channelBuilder("jms-subscribe", "jms://subscribe").setConnector(connector)); //We send on a publish queue bind(channelBuilder("jms-publish", "jms://publish").setConnector(connector)); //Bind our test bean bind(JmsBean.class).asEagerSingleton(); } }
Modules in iBeans need to extend AbstractGuiceIBeansModule which provides a single method doConfigure where any wiring logic needs to be executed. In this example we configure two JMS channels. The channels are crated using a channelBuilder that provides an easy way to create a channel with a connector, transformers and filter pre-configured. The bind method will bind an object to the current application context. Here the JMS channels will be bound to "jms-subscribe" and "jms-publish"
Finally, we bind the JmsBean as a singleton to the application context. the JmsBean is an iBeans annotated object that uses the JMS channels.
public class JmsBean { @Receive(config = "jms-subscribe") @Send(config = "jms-publish") public Object onMessage(Object message) { return message; } }
You also need to tell iBeans that you are using Guice. To do this you simply need to add a configuration parameter 'config.builder' to the IBeansServlet in your web.xml -
<servlet> <servlet-name>mule-ibeans</servlet-name> <servlet-class>org.mule.ibeans.web.IBeansServlet</servlet-class> <init-param> <param-name>config.builder</param-name> <param-value>guice</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
| When using Guice modules, any objects annotated with iBeans annotations need to be explicitly bound using the bind method. It is currently not possible to use Guice and have iBeans scan for your annotated classes. |
We recommend you familiarize yourself with Guice, the user guide is a quick read. Spring users should take a look at this but note that iBeans supports Spring too.
Configuring Transformers
When using Guice you must explicitly bind your transformer class, do this using the bind method and binding the transformer class as an eager singleton.
public MyConfigModule extends AbstractGuiceIBeansModule { protected void doConfigure() throws Exception { ... //Bind your transformers bind(MyTransformers.class).asEagerSingleton(); } }
Injecting iBeans
When using Guice it is still possible to use the @IntegrationBean annotation to inject an iBean instance into your object. There is no need to do anything different.
Add Comment