Friday, November 6, 2009

Mysore Maliga Bf Film

A bit 'of ... Guice and EJB3, very exotic! - Part 2


We're back to try the wonders of Guice. My goal is to be injected into EJB3 Session Bean non-managed, as POJO. The annotation for EJB3 standard @ Iniection Dependency is bad, then we have to work around this limitation. But let's step by step.

What, then, Guice specifically? a framework that allows us to configure, instantiate and inject object graph, so similar to Spring but with important differences. One of these is that Guice code is configured, is leaning on a Inglese-like syntax and does not depend on the configuration xml file. We start with an example.

I create an abstract class MyClass is a class that extends MyClass MyClassB this:

 package org.exquisitus; 

public abstract class MyClass {Abstract void

yellPlease (String str);}


 package org.exquisitus; 

MyClassB public class MyClass extends {@ Override


yellPlease void (String str) {
System.out.format (" You yell: -% s - \\ n ", str);}

}

NiceClass Without this we write a class that I use an instance of the class MyClassB from a reference of type MyClass

 org.exquisitus package; 

import com.google.inject.Inject;

public class NiceClass

{@ Inject private MyClass a;

callMethod public void () {
a.yellPlease ("OMG!");
}}


so far everything is very simple. callMethod () will invoke 'method yellPlease object (which does not yet know what will') refers to 'a'.
No one has defined 'a', there are no setter, and there is no 'class constructor. It appears, however, 'a particular entry called "@ Iniect. And here comes Guice.

 package org.exquisitus; 

import com.google.inject.AbstractModule;

AbstractModule {public class extends GuiceModule


@ Override protected void configure () {

bind (MyClassA.class). To (MyClassB . class);}


}

GuiceModule is a class that extends AbstractModule, in particular the method configure (). This is the way to configure Guice-dependencies of our object graphs. Through
syntax bind (nomeclasse.class). To (altraclasse.class) Guice to say that every time it encounters a reference to an annotation @ Iniect MyClass, to populate a new instance of object MyClassB.
There 's also a way to tell Guice the scope of the object to instantiate, for example if I want to instantiate a singleton object, but will speak of this' more' below.
Now we just need to test your code. Create a Main:

 package org.exquisitus; 

import com.google.inject.Guice;
import com.google.inject.Injector;

TryGuicePlease {public class

public static void main (String [] args) {

System.out.format ("Let's Try [% s] please --- \\ n \\ n ", Guice.class.getName ());

Guice.createInjector Injector injector = (new GuiceModule ());

NiceClass nice = injector.getInstance (NiceClass.class)

nice.callMethod ();


}}

Et voila '! We met the injector. There 's also a way to inject the injector, but here we go sull'esotico and now we are not interested. What interests us, and 'that just require an instance of a NiceClass Guice, Guice ce instanzierà with the dependencies that are already configured. Then inside the private field will not have 'the default value will be null but rather an instance of type MyClassB, as we have indicated. And here's the result: Let's Try

 [com.google.inject.Guice] please --- 

You yell: - OMG! -

Perfect, but now things get really interesting . Let's say you need to instantiate different objects for the same type of reference. Thus adding 'MyClassC class that extends the abstract class MyClass.

 package org.exquisitus; 

MyClassC public class MyClass extends {@ Override


yellPlease void (String str) {
System.out.format ("You yell reverse: -% s - \\ n", new StringBuilder (str). Reverse ());}


}

whose purpose sympathy and shout to the contrary :-) How do I tell Guice that when I encounter a reference to the MyClass class C instead of B? Should I use a selector . There are several ways to create a selector and a very nice way is to use a remark! Well then write notes for our two classes: @ e @ ReverseYell Yell

 package org.exquisitus; 

java.lang.annotation.Retention import, import
java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;

import com.google.inject.BindingAnnotation;

BindingAnnotation @ @ Target (FIELD) @ Retention (RetentionPolicy.RUNTIME)

public @ interface Yell

} {

and write exactly the same record but this call @ ReversedYell (note the @ Target (FIELD), I'm just using this for now, do not use annotations on methods.)

now and reconfigure it using the annotatedWith () to map the newly created records as selectors for our classes and MyClassB MyClassC:

 org.exquisitus package; 

import com.google.inject.AbstractModule;

AbstractModule {public class extends GuiceModule


@ Override protected void configure () {

bind (MyClassA.class). AnnotatedWith (Yell.class). To (MyClassB. class);

bind (MyClassA.class). annotatedWith (ReverseYell.class). to (MyClassC.class);}


}

the two objects instantiated in the same class but NiceClass starting 'the same kind of reference MyClass and invoke the method in two callMethod.

 package org.exquisitus; 

import com.google.inject.Inject;

public class NiceClass

{@ Inject Yell private MyClass @ a;

@ @ Inject private MyClass ReverseYell a2;

callMethod public void () {
a.yellPlease ("OMG!");
a2.yellPlease ("OMG!");

}}

and behold the result! Let's Try

 [com.google.inject.Guice] please --- 

You yell: - OMG! - Reverse
You yell: -! GMO -

This is very interesting but not so much for our purposes. Why? What we need to get is to inject the EJB3 who already have two different types of interface (local and remote) and depending on how well written a common interface and implementation. Add record increases the code. Then there 's also the possibility' of using bundled Guice annotation called @ Named (and then 'matter of taste ...). Using the configuration module would be this:

 bind (MyClassA.class). AnnotatedWith (Names.named ("Reverse")). To (MyClassC.class) 

and to use it

 @ Inject @ Named ("Reverse") private MyClass a2; 

Here. We are now ready to delve into JBoss using Guice and discover the wonders of using Guice JNDI with the next post :-) part 3.

0 comments:

Post a Comment