You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Célio Cidral Junior <cc...@gmail.com> on 2007/05/23 20:58:03 UTC

[S2] How to get an object from Spring inside an interceptor

Hi,

I want to implement an OpenInView pattern interceptor involving
Hibernate, but there's a problem. My SessionFactory is both created
and managed by Spring, and my interceptor should have access to that
SessionFactory, however I don't know how to do that. I have already
searched a lot the web and the docs but couldn't find an answer. Does
anybody know if that's possible (and how)?.

Regards,

Célio.

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


Re: [S2] How to get an object from Spring inside an interceptor

Posted by Célio Cidral Junior <cc...@gmail.com>.
2007/5/23, Guillaume Carré <gu...@gmail.com>:
> if your SessionFactory is injected in your action, and all your
> actions extend "AbstractAction" for example, that has a
> getSessionFactory() method, you can do this:
>
> public String intercept(ActionInvocation invocation) throws Exception {
>     Action action = (Action) invocation.getAction();
>     if (action instanceof AbstractAction) {
>         sessionFactory = ((AbstractAction) action).getSessionFactory();
> ...
> }

The actions are unaware of any Hibernate type.  This is mandatory
since I don't want my actions to depend on a specific persistence
mechanism.  They only see DAO interfaces (which are also not dependent
on any persistence mechanism).

Thanks,

Célio.

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


Re: [S2] How to get an object from Spring inside an interceptor

Posted by Guillaume Carré <gu...@gmail.com>.
2007/5/23, Célio Cidral Junior <cc...@gmail.com>:
> Hi,
>
> I want to implement an OpenInView pattern interceptor involving
> Hibernate, but there's a problem. My SessionFactory is both created
> and managed by Spring, and my interceptor should have access to that
> SessionFactory, however I don't know how to do that. I have already
> searched a lot the web and the docs but couldn't find an answer. Does
> anybody know if that's possible (and how)?.

if your SessionFactory is injected in your action, and all your
actions extend "AbstractAction" for example, that has a
getSessionFactory() method, you can do this:

public String intercept(ActionInvocation invocation) throws Exception {
    Action action = (Action) invocation.getAction();
    if (action instanceof AbstractAction) {
        sessionFactory = ((AbstractAction) action).getSessionFactory();
...
}
-- 
Guillaume Carré

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


Re: [S2] How to get an object from Spring inside an interceptor

Posted by Josh Vickery <jo...@vickeryj.com>.
Not the most elegant solution, but you can pull the ApplicationContext
from the ServletContext:

ServletContext sc = ServletActionContext.getServletContext();
(ApplicationContext)sc.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");

Josh

On 5/23/07, Célio Cidral Junior <cc...@gmail.com> wrote:
> Hi,
>
> I want to implement an OpenInView pattern interceptor involving
> Hibernate, but there's a problem. My SessionFactory is both created
> and managed by Spring, and my interceptor should have access to that
> SessionFactory, however I don't know how to do that. I have already
> searched a lot the web and the docs but couldn't find an answer. Does
> anybody know if that's possible (and how)?.
>
> Regards,
>
> Célio.
>
> ---------------------------------------------------------------------
> 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