You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Smith <ss...@mainstreamdata.com> on 2011/11/10 21:06:44 UTC

Struts 2 Initialization Plugin

In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.

I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).

Any better solutions?

Re: Struts 2 Initialization Plugin

Posted by Li Ying <li...@gmail.com>.
I think lazy initialization is a simple choice, because it is not
dependent on the J2EE container, this make your code easy to test.

If you worry about duplicated-initialization triggered by multi
request, the simple solution is, make your initialization code
[synchronized], using reserved word [synchronized] of Java language.

However, if your initialization process is too heavy, the lazy
initialization mode will make the first access slow.
If you care about this problem, the [ServletContextListener] and
[initialization servlet] is a better choice. As Eric and Chris said,
they can be good entry point to call your initialization process.

I suggest:
(1)implement your initialization process as a util-class or something,
so you can do unit test, out of the J2EE context.
(2)call the initialization process from [ServletContextListener], or
[initialization servlet], or some lazy initialize code as you want.

The basic principle is:
(1)implement the real business logic in an independent mode
(2)create some adapter code, to connect your business logic and your
run time context




2011/11/11 Scott Smith <ss...@mainstreamdata.com>:
> In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.
>
> I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).
>
> Any better solutions?
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Initialization Plugin

Posted by Chris Pratt <th...@gmail.com>.
Look into the ServletContextListener interface.  It's a nice way to
initialize/dispose of one-time resources in any web app, Struts or not.
  (*Chris*)

On Thu, Nov 10, 2011 at 12:06 PM, Scott Smith <ss...@mainstreamdata.com>wrote:

> In struts 1, I used org.apache.struts.action.PlugIn as a way to create an
> object at web app startup and put it into the application context so that
> all sessions had access to it.  What's the equivalent method in Struts2?
>  That is, how can I have an object created at web application startup.
>
> I guess the alternative is lazy initialization (first guy who tries to
> access it and doesn't find it, creates it, and saves it into the app
> context; down side is I might end up with several sessions trying to create
> it until one finally makes it to the app context).
>
> Any better solutions?
>

RE: Struts 2 Initialization Plugin

Posted by Scott Smith <ss...@mainstreamdata.com>.
Thanks for all the great suggestions.  I think the spring suggestions will work best for what I want.

-----Original Message-----
From: Ken McWilliams [mailto:ken.mcwilliams@gmail.com] 
Sent: Thursday, November 10, 2011 1:40 PM
To: user@struts.apache.org
Subject: Re: Struts 2 Initialization Plugin

The better solution is dependency injection with Spring, use the
struts2-spring-plugin.

On Thu, 2011-11-10 at 15:09 -0500, Eric Reed wrote:
> You should have an initialization servlet run at startup that can create such an object.
> 
> 
> >>> Scott Smith <ss...@mainstreamdata.com> 11/10/2011 3:06 PM >>>
> In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.
> 
> I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).
> 
> Any better solutions?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Initialization Plugin

Posted by Ken McWilliams <ke...@gmail.com>.
The better solution is dependency injection with Spring, use the
struts2-spring-plugin.

On Thu, 2011-11-10 at 15:09 -0500, Eric Reed wrote:
> You should have an initialization servlet run at startup that can create such an object.
> 
> 
> >>> Scott Smith <ss...@mainstreamdata.com> 11/10/2011 3:06 PM >>>
> In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.
> 
> I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).
> 
> Any better solutions?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Initialization Plugin

Posted by Maurizio Cucchiara <ma...@gmail.com>.
You could take advantage of struts dependency injection [1].
You can use servlet filter or something else.
Generally this kind of things are easy to realize thank to the
interceptors facility.



[1] http://struts.apache.org/2.x/docs/bean-configuration.html
Maurizio Cucchiara



On 10 November 2011 21:09, Eric Reed <ER...@mail.nysed.gov> wrote:
> You should have an initialization servlet run at startup that can create such an object.
>
>
>>>> Scott Smith <ss...@mainstreamdata.com> 11/10/2011 3:06 PM >>>
> In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.
>
> I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).
>
> Any better solutions?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Initialization Plugin

Posted by Ken McWilliams <ke...@gmail.com>.
The better solution is dependency injection with Spring, use the
struts2-spring-plugin.

On Thu, 2011-11-10 at 15:09 -0500, Eric Reed wrote:
> You should have an initialization servlet run at startup that can create such an object.
> 
> 
> >>> Scott Smith <ss...@mainstreamdata.com> 11/10/2011 3:06 PM >>>
> In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.
> 
> I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).
> 
> Any better solutions?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 2 Initialization Plugin

Posted by Eric Reed <ER...@MAIL.NYSED.GOV>.
You should have an initialization servlet run at startup that can create such an object.


>>> Scott Smith <ss...@mainstreamdata.com> 11/10/2011 3:06 PM >>>
In struts 1, I used org.apache.struts.action.PlugIn as a way to create an object at web app startup and put it into the application context so that all sessions had access to it.  What's the equivalent method in Struts2?  That is, how can I have an object created at web application startup.

I guess the alternative is lazy initialization (first guy who tries to access it and doesn't find it, creates it, and saves it into the app context; down side is I might end up with several sessions trying to create it until one finally makes it to the app context).

Any better solutions?


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org