Re: Injecting singleton beans into Servlets

2008-07-09 Thread Musachy Barroso
Short anwser, no. You can use @Inject when that class is instantiated
by the container(S2 internal Guice in this case), but servlets are
instantiated by the servlet container.

musachy

On Wed, Jul 9, 2008 at 10:11 AM, Kleiderman, Matthew
<[EMAIL PROTECTED]> wrote:
> Not quite, I'm trying to keep Spring out of the mix - I have a couple of
> classes that manage access to remote services.  They get defined in
> WEB-INF/classes/struts.xml as singletons, and injected into Actions via
> @Inject annotations.
>
> Now I've realized that I need to give one of these classes a reference
> to one of the others during application startup, before any Actions get
> executed. I thought of using a Servlet's init method to handle it but
> couldn't figure out how to get the references from Struts.
>
> The less-pleasant alternative that comes to mind is to just make all of
> these classes explicit singletons, and let Actions get them via
> getInstance() calls, but that's a fair amount of rework.
>
> - Matt
>
> -Original Message-
> From: Gabriel Belingueres [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 08, 2008 7:56 PM
> To: Struts Users Mailing List
> Subject: Re: Injecting singleton beans into Servlets
>
> 2008/7/8 Kleiderman, Matthew <[EMAIL PROTECTED]>:
>> Is there any way to inject beans defined in struts.xml with
>> scope=singleton into a Servlet so that it's available when the
>> Servlet's init method is called?
>
> Did you really meant injecting a bean defined inside Spring's
> applicationContext.xml file with scope="singleton" into a Servlet? If
> so, the answer is yes:
>
> public void init(FilterConfig filterConfig) throws ServletException {
>  ...
>  ServletContext servletContext = filterConfig.getServletContext();
>  WebApplicationContext context =
> WebApplicationContextUtils.getWebApplicationContext(servletContext);
>  // set up an injected bean into application scope
>  servletContext.setAttribute("mybean", context.getBean("aBean"));
>  ...
> }
>
> Alternately, is there a way to access where
>> these instances are stored so I can get a reference to the instance?
>>
>> Thanks,
>>
>> Matt Kleiderman
>>
>> -
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Injecting singleton beans into Servlets

2008-07-09 Thread Kleiderman, Matthew
Not quite, I'm trying to keep Spring out of the mix - I have a couple of
classes that manage access to remote services.  They get defined in
WEB-INF/classes/struts.xml as singletons, and injected into Actions via
@Inject annotations.

Now I've realized that I need to give one of these classes a reference
to one of the others during application startup, before any Actions get
executed. I thought of using a Servlet's init method to handle it but
couldn't figure out how to get the references from Struts.

The less-pleasant alternative that comes to mind is to just make all of
these classes explicit singletons, and let Actions get them via
getInstance() calls, but that's a fair amount of rework.

- Matt

-Original Message-
From: Gabriel Belingueres [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 08, 2008 7:56 PM
To: Struts Users Mailing List
Subject: Re: Injecting singleton beans into Servlets

2008/7/8 Kleiderman, Matthew <[EMAIL PROTECTED]>:
> Is there any way to inject beans defined in struts.xml with 
> scope=singleton into a Servlet so that it's available when the 
> Servlet's init method is called?

Did you really meant injecting a bean defined inside Spring's
applicationContext.xml file with scope="singleton" into a Servlet? If
so, the answer is yes:

public void init(FilterConfig filterConfig) throws ServletException {
  ...
  ServletContext servletContext = filterConfig.getServletContext();
  WebApplicationContext context =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
  // set up an injected bean into application scope
  servletContext.setAttribute("mybean", context.getBean("aBean"));
  ...
}

Alternately, is there a way to access where
> these instances are stored so I can get a reference to the instance?
>
> Thanks,
>
> Matt Kleiderman
>
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Injecting singleton beans into Servlets

2008-07-08 Thread Gabriel Belingueres
2008/7/8 Kleiderman, Matthew <[EMAIL PROTECTED]>:
> Is there any way to inject beans defined in struts.xml with
> scope=singleton into a Servlet so that it's available when the Servlet's
> init method is called?

Did you really meant injecting a bean defined inside Spring's
applicationContext.xml file with scope="singleton" into a Servlet? If
so, the answer is yes:

public void init(FilterConfig filterConfig) throws ServletException {
  ...
  ServletContext servletContext = filterConfig.getServletContext();
  WebApplicationContext context =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
  // set up an injected bean into application scope
  servletContext.setAttribute("mybean", context.getBean("aBean"));
  ...
}

Alternately, is there a way to access where
> these instances are stored so I can get a reference to the instance?
>
> Thanks,
>
> Matt Kleiderman
>
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]