Contact Us 1-800-596-4880

@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 -

public class MyComponent {
    @Lookup
    private MuleContext muleContext;
    . . .
}

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.

Optional lookups

Finally, lookups are required by default, if a lookup returns null an exception will be thrown. If you want a null to be returned, just set the optional flag -

public class MyComponent3 {
    @Lookup(value = "api.key", optional = true)
    private String apiKey;
    . . .
}