struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From foo bar <>
Subject Struts2 and Guice
Date Sat, 25 Dec 2010 04:19:14 GMT
Hi all,

This is somewhat related to Ben McCann's thread on (Updating the Struts 2 Guice plugin), which I
included below.

I trying to figure out how to properly integrate quartz scheduler with
my struts2 + guice project.
My quartz job need to have a reference to the Guice injector.

I'm using guice-2.0.jar and guice-servlet-2.0.jar, using GuiceFilter on web.xml

My scheduled job looks like this
public void execute(JobExecutionContext context) throws JobExecutionException {
    // get a reference to the injector somehow
    Injector injector = ...
	CategoryService categoryService = injector.getInstance(CategoryService.class);

When I start my container (tomcat) I observed these events in,
- a GuiceObjectFactory instance is created
- @Inject(value = "guice.module", required = false) void
setModule(String moduleClassName) { ... } gets called
- @Inject(value = "struts.devMode", required = false) void
setDevelopmentMode(String developmentMode) { ... } gets called

The injector is created during the first call to
GuiceObjectFactory.buildBean(). Going to the login page (or any page)
will trigger buildBean() since it needs to create the Action, Result
instances, etc.
Here lies my problem, if my scheduled job fires before buildBean() is
called, there is no injector.

How do a quartz scheduled job get a reference to the injector then ?

My nasty hack now is create the injector in
GuiceObjectFactory.setModule(), that should reduce the likelihood of
my scheduled job running without an injector being created first.

The new way ie. Struts2Factory seems to be better.
Some questions about it (probably for Ben McCann)
- Struts2Factory should work fine with guice-2 ?
- What the user only need to do is create a ContextListener that
extends GuiceServletContextListener ? then setup my module there ?
- I notice the StrutsPrepareAndExecuteFilter is being setup in the
does this mean I do not need to include that filter in web.xml ? Or is
this only when I am using guice-3 ?

This seems to be better cause the injector is being set as an
attribute in servletContext.
I'm using xml to describe my job and trigger
how I'm supposed to pass this servletContext/injector to the
JobExecutionContext of my job is another story, worst case I'll just
use a static variable

I can of course create another injector just for my scheduled jobs,
but that will complicate my Hibernate setup since I will have 2
EntityManagerFactory, app wont work properly when I turn on caching.


On Fri, Dec 3, 2010 at 11:33 AM, Ben McCann <> wrote:
> Hi,
> We just updated the Struts 2 Guice plugin so that it will be compatible with
> Guice 3.  However, the main class of the plugin,
> Struts2Factory<>,
> is being created via xwork's injection mechanism and then statically
> injected by Guice so that we can get a reference to the Guice injector.
>  We'd like to avoid static injection whenever possible, so I'm wondering if
> anyone more familiar with Struts/XWork has a better idea.
> In struts-plugin.xml<>we
> overrode the default ObjectFactory:
>  <bean type="com.opensymphony.xwork2.ObjectFactory"
>        name="guice"
>        class=""/>
> We now require that the user create a
> Struts2GuicePluginModule<>when
> setting
> up their GuiceServletContextListener<>
> and
> this will statically inject the Guice Injector into the
> Struts2Factory<>
> .
> Is there anyway to set the Struts2Factory as the Object factory without
> specifying it in the xml?  It be nice if we could do this in code somehow so
> that we could provide an instance of the Guice injector.  Or perhaps after
> the object factory is created, is there some way that we can get a reference
> to it and manually insert the injector ourselves to avoid the use of static
> injection?
> Thanks,
> Ben

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message