Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After some research, it turns out that Atmosphere provides (currently broken) hooks for this kind of functionality. That means it <strong>IS</strong> possible to simply use your normal annotations and have it work with Atmosphere, despite the 'foreign' instantiation.</p> <p>If you know where your code will be deployed, you can simply overwrite the default noop InjectorProvider class from Atmosphere by having a class with the same name in the same package and having it provide the proper Injector.</p> <p>I am adding code for JBoss AS7 at the end of this answer, as well as links to code that is supposed to work on Google Guice and Spring, which I have not tested myself.</p> <p>If you need your code to work on multiple platforms, you will probably need to figure out how to detect what you are running on and then return the appropriate Injector. Since I'm not very familiar with Guice and Spring, I'll leave that exercise to the reader.</p> <p>Dirty 'first draft' code for JBoss AS7 (remember that this has to go into the declared package to overwrite the default noop provider):</p> <pre><code>package org.atmosphere.di; import java.util.NoSuchElementException; import java.util.ServiceLoader; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.InjectionTarget; import javax.naming.InitialContext; import javax.naming.NamingException; public class InjectorProvider { private InjectorProvider() {} public static Injector getInjector() { return LazyProvider.INJECTOR; } private static final class LazyProvider { private static final Injector INJECTOR; static { Injector injector = new Injector() { @Override public void inject(final Object o) { try { final BeanManager bm = (BeanManager) new InitialContext().lookup("java:comp/BeanManager"); final CreationalContext cc = bm.createCreationalContext(null); final InjectionTarget it = bm.createInjectionTarget(bm.createAnnotatedType(o.getClass())); it.inject(o, cc); cc.release(); } catch (final NamingException e) { e.printStackTrace(); } } }; try { injector = ServiceLoader.load(Injector.class).iterator().next(); } catch (final NoSuchElementException e) {} INJECTOR = injector; } } } </code></pre> <p>Please note that the wrapping code is taken from the Atmosphere code base, and is a very bad way to do Singletons in Java. You probably don't want to use this 'as is' in production.</p> <p>Guice injector, untested: <a href="https://github.com/Atmosphere/atmosphere/blob/atmosphere-1.0.x/extras/guice/src/main/java/org/atmosphere/guice/GuiceInjector.java" rel="nofollow">https://github.com/Atmosphere/atmosphere/blob/atmosphere-1.0.x/extras/guice/src/main/java/org/atmosphere/guice/GuiceInjector.java</a></p> <p>Spring injector, untested: <a href="https://github.com/Atmosphere/atmosphere/blob/atmosphere-1.0.x/extras/spring/src/main/java/org/atmosphere/spring/SpringInjector.java" rel="nofollow">https://github.com/Atmosphere/atmosphere/blob/atmosphere-1.0.x/extras/spring/src/main/java/org/atmosphere/spring/SpringInjector.java</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload