ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
1924
1924
EMPFEHLEN
-
03.05.07 00:41 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
hier mal ein kleines Beispiel dazu wie man mit Googles neuem Dependency Injection Framework Guice
( http://code.google.com/p/google-guice/ ) auch Dependencies von Objekten verwalten kann die nicht über den Container sonder per
new (...) erzeugt werden. Dazu bedienen wir uns ein klein wenig der Technik des Springframeworks ( http://springframework.org/ )
in Verbindung mit AspectJ in dem wir einen Configuration Aspect schreiben, der die Konstruktor-
aufrufe abfängt und die daraus resultierenden Instanzen entsprechend konfiguriert.
Da Guice scheinbar an einigen Stellen noch nicht so gut mit generics klar kommt (oder ich sie falsch
Benutze hab ich diese aus meinem ursprünglichen Beispiel mal entfernt): Das Problem wird gerade hier:
http://groups.google.com/group/googl...8924f9434bfa6e diskutiert.
Hier das Beispiel:
Unser IService-Interface
Code java:1 2 3 4 5 6 7 8 9 10 11 12
/** * */ package de.tutorials.services; /** * @author Thomas.Darimont * */ public interface IService/*<TRequest extends IRequest, TResponse extends IResponse>*/{ IResponse /*TResponse*/ process(IRequest /*TRequest*/ request); }
Unser IRequest Interface:
Code java:
Unser IResponse-Interface
Code java:
Unser MockService:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/** * */ package de.tutorials.services.internal.mock; import com.google.inject.Singleton; import de.tutorials.services.IRequest; import de.tutorials.services.IResponse; import de.tutorials.services.IService; /** * @author Thomas.Darimont * */ @Singleton public class MockService implements IService /*<MockRequest, MockResponse>*/{ public IResponse /*MockRequest*/ process(IRequest /*MockResponse*/ request) { MockResponse response = new MockResponse(); response.setResult(request.getData().toUpperCase()); return response; } }
Unser MockReuqest:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/** * */ package de.tutorials.services.internal.mock; import de.tutorials.services.IRequest; /** * @author Thomas.Darimont * */ public class MockRequest implements IRequest { String data; /** * @return the data */ public String getData() { return data; } /** * @param data the data to set */ public void setData(String data) { this.data = data; } }
Unsere MockResponse:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/** * */ package de.tutorials.services.internal.mock; import de.tutorials.services.IResponse; /** * @author Thomas.Darimont * */ public class MockResponse implements IResponse { String result; /** * @return the result */ public String getResult() { return result; } /** * @param result the result to set */ public void setResult(String result) { this.result = result; } }
unser Guice-Modle:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/** * */ package de.tutorials; import com.google.inject.Binder; import com.google.inject.Module; import de.tutorials.services.IService; import de.tutorials.services.internal.mock.MockService; /** * @author Thomas.Darimont * */ public class MainModule implements Module { public void configure(Binder binder) { binder.bind(IService.class).to(MockService.class); } }
unser Client der einen IService injected bekommen soll welcher die Configurable Annotation hat:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/** * */ package de.tutorials; import com.google.inject.Inject; import de.tutorials.configuration.Configurable; import de.tutorials.services.IRequest; import de.tutorials.services.IResponse; import de.tutorials.services.IService; import de.tutorials.services.internal.mock.MockRequest; /** * @author Thomas.Darimont * */ @Configurable public class Client { IService /*<IRequest, IResponse>*/ service; /** * @return the service */ public IService/*<IRequest, IResponse>*/ getService() { return service; } /** * @param service * the service to set */ @Inject public void setService(IService/*<IRequest, IResponse>*/ service) { System.out.println("setService"); this.service = service; } public void start() { IRequest request = new MockRequest(); request.setData("bubu"); IResponse response = getService().process(request); System.out.println(response.getResult()); } }
Unsere @Configurable-Annotation:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/** * */ package de.tutorials.configuration; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author Thomas.Darimont * */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Inherited public @interface Configurable{ }
Unser ConfigurationAspect:
die beanCreation Pointcutdefinition ist von Spring geklaut... außerdem ist es noch etwas unschönCode java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/** * */ package de.tutorials.configuration.aspects; import de.tutorials.Main; /** * @author Thomas.Darimont * */ public aspect Configuration { protected pointcut beanCreation(Object beanInstance) : initialization((@de.tutorials.configuration.Configurable *).new(..)) && this(beanInstance) && !cflow(call(* com.google.inject..*(..))); after(Object beanInstance) returning : beanCreation(beanInstance) { System.out.println("Configuring " + beanInstance.getClass() ); Main.INJECTOR.injectMembers(beanInstance); } }
den INJECTOR "global" als Klassenvariable verfügbar zu machen... na ja ist ja nur ein einfaches
Beispiel
Hier unsere Main:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/** * */ package de.tutorials; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; import de.tutorials.services.IService; /** * @author Thomas.Darimont * */ public class Main { //Bootstrap Guice Runtime public final static Injector INJECTOR = Guice.createInjector(new MainModule()); /** * @param args */ public static void main(String[] args) { //Start Client INJECTOR.getInstance(Client.class).start(); //new Client().start(); } }
Ausgabe:
Code :1 2
setService BUBU
Starten wir das ganze so:
So erhalten wir die Ausgabe:Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/** * */ package de.tutorials; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; import de.tutorials.services.IService; /** * @author Thomas.Darimont * */ public class Main { //Bootstrap Guice Runtime public final static Injector INJECTOR = Guice.createInjector(new MainModule()); /** * @param args */ public static void main(String[] args) { //Start Client //INJECTOR.getInstance(Client.class).start(); new Client().start(); } }
Code :1 2 3
Configuring class de.tutorials.Client setService BUBU
War ja richtig einfach
Um das Beispiel ausführen zu können braucht ihr die neuste Version von Guice und von AspectJ bzw. das AJDT...
Für Eclipse gibts dazu zusätzlich noch eine komplette Entwicklungsumgebung (AJDT AspectJ Development Tools
dazu: http://www.eclipse.org/ajdt/)
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
Dependency Injection Frameworks für .Net
Von Thomas Darimont im Forum .NET CaféAntworten: 0Letzter Beitrag: 18.01.10, 19:11 -
Beispiel zu Dependency Injection mit GWT & Springframework ME
Von Konstantin Denerz im Forum JavaAntworten: 0Letzter Beitrag: 21.04.09, 13:54 -
Dependency Injection & Security
Von misiu im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 4Letzter Beitrag: 17.05.08, 21:31 -
Beispiel zur Dependency Injection in Eclipse RCP View mit AspectJ und Spring
Von Thomas Darimont im Forum JavaAntworten: 8Letzter Beitrag: 17.02.08, 11:04 -
Guice - Neues Dependency Injection Framework von Google
Von Thomas Darimont im Forum JavaAntworten: 0Letzter Beitrag: 12.03.07, 13:21






Zitieren
Login





