Author: limpbizkit
Date: Tue May 19 02:04:30 2009
New Revision: 969

Added:
    wiki/CustomInjections.wiki

Log:
Created wiki page through web user interface.

Added: wiki/CustomInjections.wiki
==============================================================================
--- (empty file)
+++ wiki/CustomInjections.wiki  Tue May 19 02:04:30 2009
@@ -0,0 +1,61 @@
+#summary Performing custom injections with type and injection listeners
+=Custom Injections=
+In addition to the standard `...@inject`-driven injections, guice includes  
hooks for custom injections. This enables Guice to host other frameworks  
that have their own injection semantics or annotations. Most developers  
won't use custom injections directly; but they may see their use in  
extensions and third-party libraries. Each custom injection requires a type  
listener, an injection listener, and registration of each.
+
+[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/TypeListener.html
  
TypeListeners]  
get notified of the types that Guice injects. Since type  
listeners are only notified once-per-type, they are the best place to run  
potentially slow operations, such as enumerating a type's members. With  
their inspection complete, type listeners may register instance listeners  
for values that get injected.
+
+[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/MembersInjector.html
  
MembersInjectors]  
and  
[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/InjectionListener.html
  
InjectionListeners] can be used to receive a callback after Guice has  
injected an instance. The instance is injected by Guice, then the members  
injectors are notified, and finally the injection listeners are notified.  
Since they're notified once-per-instance, these should execute as quickly  
as possible.
+
+==Example: Injecting a Log4J Logger==
+Guice includes built-in support for injecting `java.util.Logger` instances  
that are named using the type of the injected instance. With the type  
listener API, you can inject a `org.apache.log4j.Logger` with the same  
high-fidelity naming. We'll be injecting fields of this format:
+{{{
+import org.apache.log4j.Logger;
+import com.publicobject.log4j.InjectLogger;
+
+public class PaymentService {
+  @InjectLogger Logger logger;
+
+  ...
+}
+}}}
+We start by registering our custom type listener `Log4JTypeListener` in a  
module:
+{{{
+  @Override protected void configure() {
+    bindListener(Matchers.any(), new Log4JTypeListener());
+  }
+}}}
+You can implement the  
[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/TypeListener.html
  
TypeListener] to scan through  a type's fields, looking for Log4J loggers.  
For each logger field that's encountered, we register a  
`Log4JMembersInjector` on the passed-in  
[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/TypeEncounter.html
  
TypeEncounter]:
+{{{
+  class Log4JTypeListener implements TypeListener {
+    public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T>  
typeEncounter) {
+      for (Field field : typeLiteral.getRawType().getDeclaredFields()) {
+        if (field.getType() == Logger.class
+            && field.isAnnotationPresent(InjectLogger.class)) {
+          typeEncounter.register(new Log4JMembersInjector<T>(field));
+        }
+      }
+    }
+  }
+}}}
+Finally, implement the `Log4JMembersInjector` to set the logger. We always  
set the field to the same instance here. In your application you may need  
to compute a value or request one from a Guice provider.
+{{{
+  class Log4JMembersInjector<T> implements MembersInjector<T> {
+    private final Field field;
+    private final Logger logger;
+
+    Log4JMembersInjector(Field field) {
+      this.field = field;
+      this.logger = Logger.getLogger(field.getDeclaringClass());
+      field.setAccessible(true);
+    }
+
+    public void injectMembers(T t) {
+      try {
+        field.set(t, logger);
+      } catch (IllegalAccessException e) {
+        throw new RuntimeException(e);
+      }
+    }
+  }
+}}}
+

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to