You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by foo bar <li...@gmail.com> on 2010/12/25 05:19:14 UTC

Struts2 and Guice

Hi all,

This is somewhat related to Ben McCann's thread on
dev@struts.apache.org (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
<filter>
    <filter-name>guice</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

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
com.google.inject.struts2.GuiceObjectFactory,
- 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
ExampleListener
(http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java),
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
(http://www.quartz-scheduler.org/docs/cookbook/JobInitPlugin.html),
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.

Thanks

On Fri, Dec 3, 2010 at 11:33 AM, Ben McCann <bm...@google.com> 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<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java>,
> 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<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/struts-plugin.xml>we
> overrode the default ObjectFactory:
>  <bean type="com.opensymphony.xwork2.ObjectFactory"
>        name="guice"
>        class="com.google.inject.struts2.Struts2Factory"/>
>
> We now require that the user create a
> Struts2GuicePluginModule<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java>when
> setting
> up their GuiceServletContextListener<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java>
> and
> this will statically inject the Guice Injector into the
> Struts2Factory<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java>
> .
> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 and Guice

Posted by Frans Thamura <fr...@meruvian.org>.
The integration between s2 and guice.

There are several issues in guice. 


/m/

-----Original Message-----
From: Dave Newton <da...@gmail.com>
Date: Fri, 24 Dec 2010 23:50:07 
To: Struts Users Mailing List<us...@struts.apache.org>; <fr...@meruvian.org>
Subject: Re: Struts2 and Guice

What isn't stable enough, and where did you read it?
On Dec 24, 2010 10:41 PM, "Frans Thamura" <fr...@meruvian.org> wrote:
> I read that it is not stable enough
> /m/
>
> -----Original Message-----
> From: foo bar <li...@gmail.com>
> Date: Sat, 25 Dec 2010 15:19:14
> To: Struts Users Mailing List<us...@struts.apache.org>
> Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
> Cc: Ben McCann<bm...@google.com>
> Subject: Struts2 and Guice
>
> Hi all,
>
> This is somewhat related to Ben McCann's thread on
> dev@struts.apache.org (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
> <filter>
> <filter-name>guice</filter-name>
> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> </filter>
>
> 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
> com.google.inject.struts2.GuiceObjectFactory,
> - 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
> ExampleListener
> (
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java
),
> 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
> (http://www.quartz-scheduler.org/docs/cookbook/JobInitPlugin.html),
> 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.
>
> Thanks
>
> On Fri, Dec 3, 2010 at 11:33 AM, Ben McCann <bm...@google.com> 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<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
>,
>> 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<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/struts-plugin.xml
>we
>> overrode the default ObjectFactory:
>>  <bean type="com.opensymphony.xwork2.ObjectFactory"
>>        name="guice"
>>        class="com.google.inject.struts2.Struts2Factory"/>
>>
>> We now require that the user create a
>> Struts2GuicePluginModule<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java
>when
>> setting
>> up their GuiceServletContextListener<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java
>
>> and
>> this will statically inject the Guice Injector into the
>> Struts2Factory<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
>
>> .
>> 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: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


Re: Struts2 and Guice

Posted by Dave Newton <da...@gmail.com>.
What isn't stable enough, and where did you read it?
On Dec 24, 2010 10:41 PM, "Frans Thamura" <fr...@meruvian.org> wrote:
> I read that it is not stable enough
> /m/
>
> -----Original Message-----
> From: foo bar <li...@gmail.com>
> Date: Sat, 25 Dec 2010 15:19:14
> To: Struts Users Mailing List<us...@struts.apache.org>
> Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
> Cc: Ben McCann<bm...@google.com>
> Subject: Struts2 and Guice
>
> Hi all,
>
> This is somewhat related to Ben McCann's thread on
> dev@struts.apache.org (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
> <filter>
> <filter-name>guice</filter-name>
> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> </filter>
>
> 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
> com.google.inject.struts2.GuiceObjectFactory,
> - 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
> ExampleListener
> (
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java
),
> 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
> (http://www.quartz-scheduler.org/docs/cookbook/JobInitPlugin.html),
> 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.
>
> Thanks
>
> On Fri, Dec 3, 2010 at 11:33 AM, Ben McCann <bm...@google.com> 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<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
>,
>> 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<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/struts-plugin.xml
>we
>> overrode the default ObjectFactory:
>>  <bean type="com.opensymphony.xwork2.ObjectFactory"
>>        name="guice"
>>        class="com.google.inject.struts2.Struts2Factory"/>
>>
>> We now require that the user create a
>> Struts2GuicePluginModule<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java
>when
>> setting
>> up their GuiceServletContextListener<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java
>
>> and
>> this will statically inject the Guice Injector into the
>> Struts2Factory<
http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
>
>> .
>> 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: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

Re: Struts2 and Guice

Posted by Frans Thamura <fr...@meruvian.org>.
I read that it is not stable enough
/m/

-----Original Message-----
From: foo bar <li...@gmail.com>
Date: Sat, 25 Dec 2010 15:19:14 
To: Struts Users Mailing List<us...@struts.apache.org>
Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
Cc: Ben McCann<bm...@google.com>
Subject: Struts2 and Guice

Hi all,

This is somewhat related to Ben McCann's thread on
dev@struts.apache.org (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
<filter>
    <filter-name>guice</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

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
com.google.inject.struts2.GuiceObjectFactory,
- 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
ExampleListener
(http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java),
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
(http://www.quartz-scheduler.org/docs/cookbook/JobInitPlugin.html),
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.

Thanks

On Fri, Dec 3, 2010 at 11:33 AM, Ben McCann <bm...@google.com> 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<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java>,
> 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<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/struts-plugin.xml>we
> overrode the default ObjectFactory:
>  <bean type="com.opensymphony.xwork2.ObjectFactory"
>        name="guice"
>        class="com.google.inject.struts2.Struts2Factory"/>
>
> We now require that the user create a
> Struts2GuicePluginModule<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java>when
> setting
> up their GuiceServletContextListener<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java>
> and
> this will statically inject the Guice Injector into the
> Struts2Factory<http://code.google.com/p/google-guice/source/browse/trunk/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java>
> .
> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org