Guice Automatic Injection/Binding – GuicyFruit Integration, @PostConstruct/@PreDestroy, Guice+JNDI and the ChildInjector

Veröffentlicht: September 15, 2010 in Automatic Binding for Guice, Java
Schlagworte: , , , , , , , , ,
		<dependency>
			<groupId>de.devsurf.injection.guice.integrations</groupId>
			<artifactId>de.devsurf.injection.guice.integrations.guicyfruit</artifactId>
			<version>0.6.2-SNAPSHOT</version>
		</dependency>

This will add the JSR250Module of the GuicyFruit-Extension to your Classpath and the Scanner will find the Module, because it is marked with @GuiceModule.
So you can annotate your Classes with @PostConstruct, @PreDestroy and/or @Resource.

In my git-Repository you will also find an Example-Folder, where all Features are used. From automatic Binding of Classes, over automatic installation of Modules, to using JNDI and/or Multiple-Bindings.

I’ll show it in the next Example how to use it.

In our Example we have one Interface “Example” :) :

public interface Example {
    String sayHello(); //will be called in our Application
    void inform(); //will be called through @PostConstruct
}

Our Implementation is as easy and boring as you would expect it :) :

public class ExampleImpl implements Example {  
    @PostConstruct  
    public void inform(){  
	System.out.println("inform about post construction!");  
    }  
    
    @Override  
    public String sayHello() {  
	return "yeahhh!!!";  
    }  
}

and for completeness a Module, which will be installed automatically which will bind our Interfact to the Implementation:

@GuiceModule
public class ExampleModule extends AbstractModule {   
    @Override
    protected void configure() {  
	bind(Example.class).to(ExampleImpl.class);
    }
}

You see how difficult it is? :)

Oh and I forgot our Application. We will use the StartupModule, which only wants to know about the ClasspathScanner-Implementation and which Packages should be scanned.
After creating an Injector, we will retrieve an Instance of our DynamicModule, which is bound to the ScannerModule.
Last but not least, this Module can be used to create a new Injector.

public static void main(String[] args) throws IOException {  
	StartupModule startupModule = StartupModule.create(VirtualClasspathReader.class, ExampleApp.class.getPackage().getName(), JSR250Module.class.getPackage().getName());  
	Injector injector = Guice.createInjector(startupModule);  
  
	Module m = Modules.combine(startupModule, injector.getInstance(DynamicModule.class));  
	injector = Guice.createInjector(m); 

	System.out.println(injector.getInstance(Example.class).sayHello());  
}

Attention: At the Moment there is something I don’t understand. When I use my original Injector to create a ChildInjector, all Interface-to-Class-Bindings work like a charm. But I install a Module, which will bind a TypeListener or something else, they are not recognized.
I’m not sure if this is a Bug or a wanted behavior. So we are only able to use GuicyFruit, if we create a new Injector with the Help of the DynamicModule.

Guicefy JNDI

If you are working a lot with JNDI and/or JavaEE you often have to work with a Context. A good description about the Use Case can be found on the GuicyFruit Wiki. I’ll only describe how you can use it with my Extension.

First of all you will need a “jndi.properties” with the ContextFactory, our Class for the ClasspathScanner and which Packages should be scanned. (sounds familiar? :))

java.naming.factory.initial = ...integrations.guicyfruit.GuicyInitialContextFactory
guice.classpath.scanner = ...scanner.asm.VirtualClasspathReader
guice.classpath.packages = ...

After that you need to create a new Context and lookup your Instances with the help of it. The Rest is magic. :) (behind the scene there is the normal automatic binding and installation of modules/beans)

public static void main(String[] args) throws Exception {  
	InitialContext context = new InitialContext();  
	Example example = (Example) context.lookup(Example.class.getName());  
  	
	System.out.println(example.sayHello());  
}

On the road to Version 1.0 I have to invest a little bit in the rocoto-Integration and further Tests/Documentation.

About these ads

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ photo

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s