public class MyComponent {
@Lookup
private MuleContext muleContext;
. . .
}
@Lookup Annotation
The Lookup annotation is used to inject objects from the Mule registry. These are the objects created from the Mule configuration files. Typically it will be used to inject objects from the registry into custom components and transformers. It can be used in two ways; field injection and parameter injection.
Assumptions
This page assumes you are familiar with Java annotations (marked with an @). For a brief overview of annotations, see the Java annotations tutorial .
Field injection
Field injection can be done by type -
or by name of the object configured in the Mule configuration -
public class MyComponent2 {
@Lookup("myTransformer")
private Transformer transformer;
. . .
}
Parameter injection
This injection happens when a component entry point (method call) or when a Transformer method is called. This type of injection is less useful since most of the time you will want to inject objects during initialize phase.
public class MyTransformers {
@Transformer
public Item xmlToItem(InputStream payload, @Lookup JAXBContext jaxbContext) {
// do stuff
}
}
Here, when the transformer is invoked a pre-configured JAXBContext will be injected. The same injection works for components too.
public class MyComponent3 {
public Object process(@Payload String payload, @Lookup MuleContext muleContext) {
// do stuff
}
}
JSR-330 Annotations
If you are familiar with the javax.inject
or JSR-330 annotations you might be wondering why we didn’t use @Inject
and @Named
. These annotations are designed for Dependency Injection frameworks such as Spring or Guice. Mule is not a DI container but works with the existing ones, however, sometimes you will want to inject objects configured in Mule into your components and transformers.