You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Asleson, Ryan" <as...@BIWORLDWIDE.com> on 2008/05/08 15:29:15 UTC

Injecting Spring Services into Struts 2 Actions

 
Hello,
 
I found this guide to injecting Spring beans into Struts 2 Actions:
 
http://cwiki.apache.org/WW/spring.html
 
According to the red box at the bottom of the document, the Struts 2
Action does not need to be registered with Spring.  So far so good.
 
However, I'm a little unclear as to how exactly it "knows" what Spring
beans to inject into the Struts 2 Action.  Assuming I have a Struts 2
Action that depends on a PersonService, I assume that the Struts 2
Action would have a public mutator for the PersonService like this:
 
public void setPersonService(PersonService service) {
    this.personService = service;
}
 
How does the injection system "know" that this method should be used for
injecting a Spring bean?  Imagine that the Struts 2 Action has several
"set" methods, and that there are a number of configured Spring beans.
It would take way too long for the injection system to look through
every "set" method on the Action and try to find a matching Spring bean,
especially if there are a lot of Spring beans (and there usually are).
 
The document above includes this comment:
 
We strongly recommend that you find declarative ways of letting Spring
know what to provide for your actions.
 
But it doesn't give an example of how to let Spring know what to provide
to the actions.
 
So, the question is:  How do I tell Spring what beans need to be
injected into the Action?
 
Thanks!!!!
 
-Ryan
 
 

This e-mail message is being sent solely for use by the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by phone or reply by e-mail, delete the original message and destroy all copies. Thank you.

Re: Injecting Spring Services into Struts 2 Actions

Posted by "Alberto A. Flores" <aa...@gmail.com>.
Related to this question, I'm leveraging Spring's use of the @Resource 
annotation to the private member variable (no setter needed), thus fully 
relying on Spring's injection mechanism. I supposed it all depends on 
tight coupling to Spring you want to add.

Relph,Brian wrote:
> The actions are auto-wired by name by default - so if any of its properties match bean-ids in your spring config, and they have setters, then those properties will get set.
> 
> The part about declaring the dependencies means you disable the auto-wiring, and declare your actions as spring beans. So, instead of this:
> 
>         <action name="index"
>             class="com.actions.IndexAction">
>             <result>/WEB-INF/view/index.jsp</result>
>         </action>
> 
> You would do this:
> 
>         <action name="index"
>             class="index-action-id">
>             <result>/WEB-INF/view/index.jsp</result>
>         </action>
> 
> Along with additional spring config:
> 
>     <bean id="index-action-id"
>         class="com.actions.IndexAction">
>         <property name="myDao" ref="myDao-id" />
>         <property name="myMailSender" ref="myMailSender-id" />
>     </bean>
> 
> Notice this does not actually disable auto-wiring (you would have to set the auto-wire attribute in the spring config, either globally or per-bean), but it does declaratively state what to set on the action as properties.
> 
> Brian Relph
> 
> -----Original Message-----
> From: Asleson, Ryan [mailto:asleson@BIWORLDWIDE.com] 
> Sent: Thursday, May 08, 2008 8:29 AM
> To: Struts Users Mailing List
> Subject: Injecting Spring Services into Struts 2 Actions
> 
>  
> Hello,
>  
> I found this guide to injecting Spring beans into Struts 2 Actions:
>  
> http://cwiki.apache.org/WW/spring.html
>  
> According to the red box at the bottom of the document, the Struts 2 Action does not need to be registered with Spring.  So far so good.
>  
> However, I'm a little unclear as to how exactly it "knows" what Spring beans to inject into the Struts 2 Action.  Assuming I have a Struts 2 Action that depends on a PersonService, I assume that the Struts 2 Action would have a public mutator for the PersonService like this:
>  
> public void setPersonService(PersonService service) {
>     this.personService = service;
> }
>  
> How does the injection system "know" that this method should be used for injecting a Spring bean?  Imagine that the Struts 2 Action has several "set" methods, and that there are a number of configured Spring beans.
> It would take way too long for the injection system to look through every "set" method on the Action and try to find a matching Spring bean, especially if there are a lot of Spring beans (and there usually are).
>  
> The document above includes this comment:
>  
> We strongly recommend that you find declarative ways of letting Spring know what to provide for your actions.
>  
> But it doesn't give an example of how to let Spring know what to provide to the actions.
>  
> So, the question is:  How do I tell Spring what beans need to be injected into the Action?
>  
> Thanks!!!!
>  
> -Ryan
>  
>  
> 
> This e-mail message is being sent solely for use by the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by phone or reply by e-mail, delete the original message and destroy all copies. Thank you.
> 
> ----------------------------------------------------------------------
> CONFIDENTIALITY NOTICE This message and any included attachments are from Cerner Corporation and are intended only for the addressee. The information contained in this message is confidential and may constitute inside or non-public information under international, federal, or state securities laws. Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Cerner's corporate offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 

-- 

Alberto A. Flores
http://www.linkedin.com/in/aflores



RE: Injecting Spring Services into Struts 2 Actions

Posted by "Relph,Brian" <Br...@Cerner.com>.
The actions are auto-wired by name by default - so if any of its properties match bean-ids in your spring config, and they have setters, then those properties will get set.

The part about declaring the dependencies means you disable the auto-wiring, and declare your actions as spring beans. So, instead of this:

        <action name="index"
            class="com.actions.IndexAction">
            <result>/WEB-INF/view/index.jsp</result>
        </action>

You would do this:

        <action name="index"
            class="index-action-id">
            <result>/WEB-INF/view/index.jsp</result>
        </action>

Along with additional spring config:

    <bean id="index-action-id"
        class="com.actions.IndexAction">
        <property name="myDao" ref="myDao-id" />
        <property name="myMailSender" ref="myMailSender-id" />
    </bean>

Notice this does not actually disable auto-wiring (you would have to set the auto-wire attribute in the spring config, either globally or per-bean), but it does declaratively state what to set on the action as properties.

Brian Relph

-----Original Message-----
From: Asleson, Ryan [mailto:asleson@BIWORLDWIDE.com] 
Sent: Thursday, May 08, 2008 8:29 AM
To: Struts Users Mailing List
Subject: Injecting Spring Services into Struts 2 Actions

 
Hello,
 
I found this guide to injecting Spring beans into Struts 2 Actions:
 
http://cwiki.apache.org/WW/spring.html
 
According to the red box at the bottom of the document, the Struts 2 Action does not need to be registered with Spring.  So far so good.
 
However, I'm a little unclear as to how exactly it "knows" what Spring beans to inject into the Struts 2 Action.  Assuming I have a Struts 2 Action that depends on a PersonService, I assume that the Struts 2 Action would have a public mutator for the PersonService like this:
 
public void setPersonService(PersonService service) {
    this.personService = service;
}
 
How does the injection system "know" that this method should be used for injecting a Spring bean?  Imagine that the Struts 2 Action has several "set" methods, and that there are a number of configured Spring beans.
It would take way too long for the injection system to look through every "set" method on the Action and try to find a matching Spring bean, especially if there are a lot of Spring beans (and there usually are).
 
The document above includes this comment:
 
We strongly recommend that you find declarative ways of letting Spring know what to provide for your actions.
 
But it doesn't give an example of how to let Spring know what to provide to the actions.
 
So, the question is:  How do I tell Spring what beans need to be injected into the Action?
 
Thanks!!!!
 
-Ryan
 
 

This e-mail message is being sent solely for use by the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by phone or reply by e-mail, delete the original message and destroy all copies. Thank you.

----------------------------------------------------------------------
CONFIDENTIALITY NOTICE This message and any included attachments are from Cerner Corporation and are intended only for the addressee. The information contained in this message is confidential and may constitute inside or non-public information under international, federal, or state securities laws. Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Cerner's corporate offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024.

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


Re: Injecting Spring Services into Struts 2 Actions

Posted by Musachy Barroso <mu...@gmail.com>.
If you are refering to the annotations, yes they are an spring 2.5
thing (about time!).

musachy

On Fri, May 9, 2008 at 3:14 AM, Laurie Harper <la...@holoweb.net> wrote:
> Is what a Spring 2.5 thing? AFAIK, you need to set the action mapping's
> 'class' attribute to the Spring bean identifier to get injection via the
> spring plugin... but maybe I'm just tiered and not thinking it through :-/
>
> L.
>
> Dave Newton wrote:
>>
>> Is this a Spring 2.5 thing? I'm not using 2.5 yet and haven't had to do
>> anything to get my actions to be injected with Spring beans (at least I
>> don't think so; I remember having to set the object factory to "spring"
>> in earlier days?)
>>
>> Dave
>>
>> --- Laurie Harper <la...@holoweb.net> wrote:
>>
>>> Asleson, Ryan wrote:
>>>>
>>>>  Hello,
>>>>  I found this guide to injecting Spring beans into Struts 2 Actions:
>>>>  http://cwiki.apache.org/WW/spring.html
>>>>  According to the red box at the bottom of the document, the Struts
>>>
>>> 2
>>>>
>>>> Action does not need to be registered with Spring.  So far so good.
>>>
>>> Er, that's not as clear as it could be; I believe it means that
>>> 'registering Actions with Spring is not required *if you don't want Spring
>>> dependency injection for that action*'. In other words, you
>>> *do* need Spring to know about the action if you want it to inject
>>> dependencies.
>>>
>>>> However, I'm a little unclear as to how exactly it "knows" what
>>>
>>> Spring
>>>>
>>>> beans to inject into the Struts 2 Action.  Assuming I have a Struts
>>>
>>> 2
>>>>
>>>> Action that depends on a PersonService, I assume that the Struts 2
>>>> Action would have a public mutator for the PersonService like this:
>>>>  public void setPersonService(PersonService service) {
>>>>    this.personService = service;
>>>> }
>>>>  How does the injection system "know" that this method should be
>>>
>>> used for
>>>>
>>>> injecting a Spring bean?  Imagine that the Struts 2 Action has
>>>
>>> several
>>>>
>>>> "set" methods, and that there are a number of configured Spring
>>>
>>> beans.
>>>>
>>>> It would take way too long for the injection system to look through
>>>> every "set" method on the Action and try to find a matching Spring
>>>
>>> bean,
>>>>
>>>> especially if there are a lot of Spring beans (and there usually
>>>
>>> are).
>>>>
>>>>  The document above includes this comment:
>>>>  We strongly recommend that you find declarative ways of letting
>>>
>>> Spring
>>>>
>>>> know what to provide for your actions.
>>>>  But it doesn't give an example of how to let Spring know what to
>>>
>>> provide
>>>>
>>>> to the actions.
>>>>  So, the question is:  How do I tell Spring what beans need to be
>>>> injected into the Action?
>>>
>>> You can either configure the action as a Spring bean in your
>>> applicationContext.xml and use the normal declarative syntax to do
>>> this, or you can use Spring 2.5's @Component, @Resource, etc. annotations
>>> and skip the XML. But you do need to do one or the other.
>>>
>>> L.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>
>



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

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


Re: Injecting Spring Services into Struts 2 Actions

Posted by Dave Newton <ne...@yahoo.com>.
--- Laurie Harper <la...@holoweb.net> wrote:
> Is what a Spring 2.5 thing? AFAIK, you need to set the action
> mapping's 'class' attribute to the Spring bean identifier to get 
> injection via the spring plugin... 

Oh; I wonder why I don't have to do that.

My understanding was that they only need to be declared in the Spring
config file if you wanted to configure them manually.

{quote}
  When an object is to be created, it uses the 
  class attribute in the Struts configuration 
  to correspond to the id attribute in the Spring 
  configuration. *If not found, the class will try 
  to be created as usual, then be autowired by Spring.*
{quote}

Dave

> Dave Newton wrote:
> > Is this a Spring 2.5 thing? I'm not using 2.5 yet and haven't had
> to do
> > anything to get my actions to be injected with Spring beans (at
> least I
> > don't think so; I remember having to set the object factory to
> "spring"
> > in earlier days?)
> > 
> > Dave
> > 
> > --- Laurie Harper <la...@holoweb.net> wrote:
> > 
> >> Asleson, Ryan wrote:
> >>>  
> >>> Hello,
> >>>  
> >>> I found this guide to injecting Spring beans into Struts 2
> Actions:
> >>>  
> >>> http://cwiki.apache.org/WW/spring.html
> >>>  
> >>> According to the red box at the bottom of the document, the
> Struts
> >> 2
> >>> Action does not need to be registered with Spring.  So far so
> good.
> >> Er, that's not as clear as it could be; I believe it means that 
> >> 'registering Actions with Spring is not required *if you don't
> want 
> >> Spring dependency injection for that action*'. In other words, you
> >> *do* 
> >> need Spring to know about the action if you want it to inject
> >> dependencies.
> >>
> >>> However, I'm a little unclear as to how exactly it "knows" what
> >> Spring
> >>> beans to inject into the Struts 2 Action.  Assuming I have a
> Struts
> >> 2
> >>> Action that depends on a PersonService, I assume that the Struts
> 2
> >>> Action would have a public mutator for the PersonService like
> this:
> >>>  
> >>> public void setPersonService(PersonService service) {
> >>>     this.personService = service;
> >>> }
> >>>  
> >>> How does the injection system "know" that this method should be
> >> used for
> >>> injecting a Spring bean?  Imagine that the Struts 2 Action has
> >> several
> >>> "set" methods, and that there are a number of configured Spring
> >> beans.
> >>> It would take way too long for the injection system to look
> through
> >>> every "set" method on the Action and try to find a matching
> Spring
> >> bean,
> >>> especially if there are a lot of Spring beans (and there usually
> >> are).
> >>>  
> >>> The document above includes this comment:
> >>>  
> >>> We strongly recommend that you find declarative ways of letting
> >> Spring
> >>> know what to provide for your actions.
> >>>  
> >>> But it doesn't give an example of how to let Spring know what to
> >> provide
> >>> to the actions.
> >>>  
> >>> So, the question is:  How do I tell Spring what beans need to be
> >>> injected into the Action?
> >> You can either configure the action as a Spring bean in your 
> >> applicationContext.xml and use the normal declarative syntax to do
> >> this, 
> >> or you can use Spring 2.5's @Component, @Resource, etc.
> annotations
> >> and 
> >> skip the XML. But you do need to do one or the other.


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


Re: Injecting Spring Services into Struts 2 Actions

Posted by Laurie Harper <la...@holoweb.net>.
Is what a Spring 2.5 thing? AFAIK, you need to set the action mapping's 
'class' attribute to the Spring bean identifier to get injection via the 
spring plugin... but maybe I'm just tiered and not thinking it through :-/

L.

Dave Newton wrote:
> Is this a Spring 2.5 thing? I'm not using 2.5 yet and haven't had to do
> anything to get my actions to be injected with Spring beans (at least I
> don't think so; I remember having to set the object factory to "spring"
> in earlier days?)
> 
> Dave
> 
> --- Laurie Harper <la...@holoweb.net> wrote:
> 
>> Asleson, Ryan wrote:
>>>  
>>> Hello,
>>>  
>>> I found this guide to injecting Spring beans into Struts 2 Actions:
>>>  
>>> http://cwiki.apache.org/WW/spring.html
>>>  
>>> According to the red box at the bottom of the document, the Struts
>> 2
>>> Action does not need to be registered with Spring.  So far so good.
>> Er, that's not as clear as it could be; I believe it means that 
>> 'registering Actions with Spring is not required *if you don't want 
>> Spring dependency injection for that action*'. In other words, you
>> *do* 
>> need Spring to know about the action if you want it to inject
>> dependencies.
>>
>>> However, I'm a little unclear as to how exactly it "knows" what
>> Spring
>>> beans to inject into the Struts 2 Action.  Assuming I have a Struts
>> 2
>>> Action that depends on a PersonService, I assume that the Struts 2
>>> Action would have a public mutator for the PersonService like this:
>>>  
>>> public void setPersonService(PersonService service) {
>>>     this.personService = service;
>>> }
>>>  
>>> How does the injection system "know" that this method should be
>> used for
>>> injecting a Spring bean?  Imagine that the Struts 2 Action has
>> several
>>> "set" methods, and that there are a number of configured Spring
>> beans.
>>> It would take way too long for the injection system to look through
>>> every "set" method on the Action and try to find a matching Spring
>> bean,
>>> especially if there are a lot of Spring beans (and there usually
>> are).
>>>  
>>> The document above includes this comment:
>>>  
>>> We strongly recommend that you find declarative ways of letting
>> Spring
>>> know what to provide for your actions.
>>>  
>>> But it doesn't give an example of how to let Spring know what to
>> provide
>>> to the actions.
>>>  
>>> So, the question is:  How do I tell Spring what beans need to be
>>> injected into the Action?
>> You can either configure the action as a Spring bean in your 
>> applicationContext.xml and use the normal declarative syntax to do
>> this, 
>> or you can use Spring 2.5's @Component, @Resource, etc. annotations
>> and 
>> skip the XML. But you do need to do one or the other.
>>
>> L.
>>
>>
>> ---------------------------------------------------------------------
>> 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: Injecting Spring Services into Struts 2 Actions

Posted by Dave Newton <ne...@yahoo.com>.
Is this a Spring 2.5 thing? I'm not using 2.5 yet and haven't had to do
anything to get my actions to be injected with Spring beans (at least I
don't think so; I remember having to set the object factory to "spring"
in earlier days?)

Dave

--- Laurie Harper <la...@holoweb.net> wrote:

> Asleson, Ryan wrote:
> >  
> > Hello,
> >  
> > I found this guide to injecting Spring beans into Struts 2 Actions:
> >  
> > http://cwiki.apache.org/WW/spring.html
> >  
> > According to the red box at the bottom of the document, the Struts
> 2
> > Action does not need to be registered with Spring.  So far so good.
> 
> Er, that's not as clear as it could be; I believe it means that 
> 'registering Actions with Spring is not required *if you don't want 
> Spring dependency injection for that action*'. In other words, you
> *do* 
> need Spring to know about the action if you want it to inject
> dependencies.
> 
> > However, I'm a little unclear as to how exactly it "knows" what
> Spring
> > beans to inject into the Struts 2 Action.  Assuming I have a Struts
> 2
> > Action that depends on a PersonService, I assume that the Struts 2
> > Action would have a public mutator for the PersonService like this:
> >  
> > public void setPersonService(PersonService service) {
> >     this.personService = service;
> > }
> >  
> > How does the injection system "know" that this method should be
> used for
> > injecting a Spring bean?  Imagine that the Struts 2 Action has
> several
> > "set" methods, and that there are a number of configured Spring
> beans.
> > It would take way too long for the injection system to look through
> > every "set" method on the Action and try to find a matching Spring
> bean,
> > especially if there are a lot of Spring beans (and there usually
> are).
> >  
> > The document above includes this comment:
> >  
> > We strongly recommend that you find declarative ways of letting
> Spring
> > know what to provide for your actions.
> >  
> > But it doesn't give an example of how to let Spring know what to
> provide
> > to the actions.
> >  
> > So, the question is:  How do I tell Spring what beans need to be
> > injected into the Action?
> 
> You can either configure the action as a Spring bean in your 
> applicationContext.xml and use the normal declarative syntax to do
> this, 
> or you can use Spring 2.5's @Component, @Resource, etc. annotations
> and 
> skip the XML. But you do need to do one or the other.
> 
> L.
> 
> 
> ---------------------------------------------------------------------
> 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: Injecting Spring Services into Struts 2 Actions

Posted by Laurie Harper <la...@holoweb.net>.
Asleson, Ryan wrote:
>  
> Hello,
>  
> I found this guide to injecting Spring beans into Struts 2 Actions:
>  
> http://cwiki.apache.org/WW/spring.html
>  
> According to the red box at the bottom of the document, the Struts 2
> Action does not need to be registered with Spring.  So far so good.

Er, that's not as clear as it could be; I believe it means that 
'registering Actions with Spring is not required *if you don't want 
Spring dependency injection for that action*'. In other words, you *do* 
need Spring to know about the action if you want it to inject dependencies.

> However, I'm a little unclear as to how exactly it "knows" what Spring
> beans to inject into the Struts 2 Action.  Assuming I have a Struts 2
> Action that depends on a PersonService, I assume that the Struts 2
> Action would have a public mutator for the PersonService like this:
>  
> public void setPersonService(PersonService service) {
>     this.personService = service;
> }
>  
> How does the injection system "know" that this method should be used for
> injecting a Spring bean?  Imagine that the Struts 2 Action has several
> "set" methods, and that there are a number of configured Spring beans.
> It would take way too long for the injection system to look through
> every "set" method on the Action and try to find a matching Spring bean,
> especially if there are a lot of Spring beans (and there usually are).
>  
> The document above includes this comment:
>  
> We strongly recommend that you find declarative ways of letting Spring
> know what to provide for your actions.
>  
> But it doesn't give an example of how to let Spring know what to provide
> to the actions.
>  
> So, the question is:  How do I tell Spring what beans need to be
> injected into the Action?

You can either configure the action as a Spring bean in your 
applicationContext.xml and use the normal declarative syntax to do this, 
or you can use Spring 2.5's @Component, @Resource, etc. annotations and 
skip the XML. But you do need to do one or the other.

L.


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