Labs Injection
Labs Injection is a Google Guice based dependency injection for Labs. It allows injecting all labs services and number of other things, set by developers.
Configuration
You can use Labs Injection in number of environments. Here is a list of HOWTOs of all available solutions.
EJB
For service injection you can use @EJB annotations, but if you want to add Labs injection to resolve other objects you have to add single interceptor in your Stateless bean @Interceptors({LabsInjectionInterceptor.class}).
So an example header will look like this
@Stateless @Local @Interceptors({LabsInjectionInterceptor.class}) public class FooServiceImpl implements FooService { // code }
This is an EJB3 @PostConstruct interceptor that injects all members of a stateless bean.
Servlets
Basically you need to call org.jboss.labs.injection.LabsInjection.getInjector().injectMembers(this); in init() method but you can just extends org.jboss.labs.servlet.LabsHttpServlet which will do this for you transparently.
JSF
Add
<variable-resolver>org.jboss.labs.injection.faces.LabsInjectionResolver</variable-resolver>
inside your <application> tag in WEB-INF/faces-config.xml. This will inject Labs members in all beans.
Seam
Use @Guice annotation from the seam-guice module. To trigger Guice injection on a Seam component, annotate it with @Guice. More information: LabsSeamGuice
Any other
If you need to inject members in some custom environment you can pass an Object you want members to inject to org.jboss.labs.injection.LabsInjection.getInjector().injectMembers(object). Remember that guice injects all the members of members as well.
Usage
Injecting members
To inject a member annotate it with com.google.inject.Injector. That's all.
@Inject private FooService fooService;
Adding injecting logic
To add your service bindings you need to modify org.jboss.labs.LabsGuiceModule. If you want to resolve JNDI components just use org.jboss.labs.injection.JNDIProvider.
For example if you want to return FooService that can be resolved with "FooServiceImpl/local" jndi string just write
public class LabsGuiceModule implements Module { public void configure(Binder binder) { binder.bind(FooService.class).toProvider(new JNDIProvider<FooService>("FooServiceImpl/local")); // other bindings } }
For more information please check Guice Guide
Comments