Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

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

Field injection

Field injection can be done by type -


         
      
1
2
3
4
5
public class MyComponent {
    @Lookup
    private MuleContext muleContext;
    . . .
}

or by name of the object configured in the Mule configuration -


         
      
1
2
3
4
5
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.


         
      
1
2
3
4
5
6
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.


         
      
1
2
3
4
5
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 -


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

@Lookup Annotation