Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way to do Guice singleton service injection into Quartz tasks, Stripes ActionBeans and ServletContextListeners?
    primarykey
    data
    text
    <p>At the moment I'm using a static singleton to obtain the singleton service that Guice 3.0 builds for me through DI - I believe this a terrible hack and not at all the intended way to use Guice for DI, and complicates unit testing.</p> <pre><code>public class ServiceProvider { private static ServiceProvider instance = new ServiceProvider(); private MyService myService; public ServiceProvider() { Injector myInjector = Guice.createInjector(new MyModule()); myService = noobmeterInjector.getInstance(MyService.class); } public static MyService getMyService() { return instance.myService; } } </code></pre> <p>However, I'm not quite sure how to fix that, as I need that service in at least four different spots:</p> <ul> <li>Stripes ActionBeans - I think the <a href="http://bgoodin.github.io/stripes-guice/" rel="nofollow">Stripes Guice plugin</a> would help me there, and I did connect it but ended up with multiple parallel MyServices</li> <li>ServletContextListener - I think possibly the <a href="http://code.google.com/p/google-guice/wiki/ServletModule" rel="nofollow">Guice Servlet module</a> can help somehow, although I'm struggling with the documentation as it mostly discusses replacing web.xml with Guice configuration (not my priority at this point) and injecting HttpServletRequests into Guice-constructed objects (again not what I want to do)</li> <li>Quartz Jobs - here I found <a href="http://www.codesmell.org/blog/2009/01/quartz-fits/" rel="nofollow">something relevant</a>, but not sure if it won't end up still creating a "parallel" MyService to the other ones</li> <li>Batch processes run from command line - this is the easy part</li> </ul> <p>The MyService service is intended to be a singleton (other services it owns open DB/MQ connections, etc., so creating multiple of these would be bad).</p> <p>Is there some simple way how to do this in Guice that I'm missing?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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