You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Peter Butler <Pe...@it-freedom.com> on 2008/10/13 19:11:52 UTC

Any way to disable autowire on struts 2 actions?

I am using Spring to create Struts 2 actions and populate the dependent
objects. This works fine when I am running my application. I am using
Struts 2.0.11.

I want to unit test my Struts 2 actions in isolation by populating the
dependencies manually using JMock objects. It seems that Struts 2
insists on autowiring actions (either by name or by type) and loading
actions with unfulfilled dependencies in my unit tests causes a "Cannot
resolve reference to bean" error.

From looking at the Struts 2 code and documentation I can't see any way
to disable this behaviour. I have tried setting default-autowire="no" in
my Spring application context configs and autowire="no" for the
individual actions in the Spring file but these settings have no effect.

Is there any way to test Struts 2 actions in isolation by populating
dependencies using JMock objects, or am I stuck with Struts' insistence
on autowiring everything?

Cheers

Peter

_______________________________________________________________________
The information contained in this e-mail is confidential and may be privileged. It is intended for the addressee only. If you are not the intended recipient, please delete this e-mail immediately. The contents of this e-mail must not be disclosed or copied without the sender's consent. The statements and opinions expressed in this message are those of the author and do not necessarily reflect those of the company. The company does not take any responsibility for the views of the author.

Registered Office: IT-Freedom Limited, 9 Minster Court, Tuscam Way, Camberley, Surrey GU15 3YY 
Registered in England, Number: 04500346
_______________________________________________________________________

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


Re: Any way to disable autowire on struts 2 actions?

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Peter Butler on 14/10/08 16:45, wrote:
>>> the recommendation is to use explicit configuration using either the
>>> Spring config file or Annotations. Why did the Struts team choose not to
>>> follow these recommendations?
> 
>> I don't think so, you can setup Spring-plugin to use autowiring by type not
>> by name and also Struts will ask Spring factory to create given bean, if it
>> can't create it, Struts will do it by self, and once again will ask Spring
>> to create dependencies and so on...
> 
> This was not my experience. If Spring could not find the appropriate type it
> would fail. If it did find the appropriate type it would auto-wire it,
> meaning I had no chance to populate it manually. For this reason I would
> strongly suggest adding an autowire="no" configuration option to
> StrutsSpringObjectFactory that allows this to happen. This would also make it
> consistent with Spring, which has an autoWire="no" option (which is also the
> default).

What would be ideal from my POV is the chance to configure the internal struts 
IoC container with extra beans.

Having to add an IoC container when struts already has one seems to be a waste 
of resources.


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


RE: Any way to disable autowire on struts 2 actions?

Posted by Peter Butler <Pe...@it-freedom.com>.
>> the recommendation is to use explicit
>> configuration using either the Spring config file or Annotations. Why did the Struts team choose
>> not to follow these recommendations?

>I don't think so, you can setup Spring-plugin to use autowiring by
>type not by name and also Struts will ask Spring factory to create
>given bean, if it can't create it, Struts will do it by self, and once
>again will ask Spring to create dependencies and so on...

This was not my experience. If Spring could not find the appropriate type it would fail. If it did find the appropriate type it would auto-wire it, meaning I had no chance to populate it manually. For this reason I would strongly suggest adding an autowire="no" configuration option to StrutsSpringObjectFactory that allows this to happen. This would also make it consistent with Spring, which has an autoWire="no" option (which is also the default).

Cheers

Peter

_______________________________________________________________________
The information contained in this e-mail is confidential and may be privileged. It is intended for the addressee only. If you are not the intended recipient, please delete this e-mail immediately. The contents of this e-mail must not be disclosed or copied without the sender's consent. The statements and opinions expressed in this message are those of the author and do not necessarily reflect those of the company. The company does not take any responsibility for the views of the author.

Registered Office: IT-Freedom Limited, 9 Minster Court, Tuscam Way, Camberley, Surrey GU15 3YY 
Registered in England, Number: 04500346
_______________________________________________________________________

Re: Any way to disable autowire on struts 2 actions?

Posted by Lukasz Lenart <lu...@googlemail.com>.
2008/10/14 Peter Butler <Pe...@it-freedom.com>:
> Just a question on the design of this: Why is the Spring object factory configured to use auto-wire?
> From what I've read (both in the Spring documentation and other sources) this is not recommended
> practice because it makes applications harder to debug - the recommendation is to use explicit
> configuration using either the Spring config file or Annotations. Why did the Struts team choose
> not to follow these recommendations?

I don't think so, you can setup Spring-plugin to use autowiring by
type not by name and also Struts will ask Spring factory to create
given bean, if it can't create it, Struts will do it by self, and once
again will ask Spring to create dependencies and so on...


Regards
-- 
Lukasz
http://www.lenart.org.pl/

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


RE: Any way to disable autowire on struts 2 actions?

Posted by Peter Butler <Pe...@it-freedom.com>.
>>> http://struts.apache.org/2.0.11/docs/spring-plugin.html
>>
>> There isn't an option here to disable autowiring
>> (unless I'm missing something).

> In struts.xml use full class names for Action not names from spring
> xml config...all Actions will be create by internal Struts class factory.

Thanks for your reply. That works, I've split my struts.xml file so that I can change the configuration. In struts.xml I'm using:

    <constant name="struts.objectFactory" value="spring" />
    <include file="struts-actions.xml" />

And in a separate struts-test.xml I'm using:

    <constant name="struts.objectFactory" value="struts" />
    <include file="struts-actions.xml" />

This way I can use the same action declarations from struts-actions.xml for both testing and running the application.

Just a question on the design of this: Why is the Spring object factory configured to use auto-wire? From what I've read (both in the Spring documentation and other sources) this is not recommended practice because it makes applications harder to debug - the recommendation is to use explicit configuration using either the Spring config file or Annotations. Why did the Struts team choose not to follow these recommendations?

Cheers

Peter

_______________________________________________________________________
The information contained in this e-mail is confidential and may be privileged. It is intended for the addressee only. If you are not the intended recipient, please delete this e-mail immediately. The contents of this e-mail must not be disclosed or copied without the sender's consent. The statements and opinions expressed in this message are those of the author and do not necessarily reflect those of the company. The company does not take any responsibility for the views of the author.

Registered Office: IT-Freedom Limited, 9 Minster Court, Tuscam Way, Camberley, Surrey GU15 3YY 
Registered in England, Number: 04500346
_______________________________________________________________________

Re: Any way to disable autowire on struts 2 actions?

Posted by Lukasz Lenart <lu...@googlemail.com>.
2008/10/14 Peter Butler <Pe...@it-freedom.com>:
>>http://struts.apache.org/2.0.11/docs/spring-plugin.html
>
> Thanks for the link. I did try this, but these are all still autowire
> options (just using different methods of autowiring - by type, by
> constructor, etc). There isn't an option here to disable autowiring
> (unless I'm missing something).


In struts.xml use full class names for Action not names from spring
xml config. In such way, your configuration not depends on Spring, you
can remove it at any time and all Actions will be create by internal
Struts class factory.


Regards
-- 
Lukasz
http://www.lenart.org.pl/

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


RE: Any way to disable autowire on struts 2 actions?

Posted by Peter Butler <Pe...@it-freedom.com>.
>> Is there any way to test Struts 2 actions in isolation by
>> populating dependencies using JMock objects, or am I stuck with
>> Struts' insistence on autowiring everything?

> Did you try one of the Spring plugin wiring settings?

>http://struts.apache.org/2.0.11/docs/spring-plugin.html

Thanks for the link. I did try this, but these are all still autowire
options (just using different methods of autowiring - by type, by
constructor, etc). There isn't an option here to disable autowiring
(unless I'm missing something).

Cheers

Peter

_______________________________________________________________________
The information contained in this e-mail is confidential and may be privileged. It is intended for the addressee only. If you are not the intended recipient, please delete this e-mail immediately. The contents of this e-mail must not be disclosed or copied without the sender's consent. The statements and opinions expressed in this message are those of the author and do not necessarily reflect those of the company. The company does not take any responsibility for the views of the author.

Registered Office: IT-Freedom Limited, 9 Minster Court, Tuscam Way, Camberley, Surrey GU15 3YY 
Registered in England, Number: 04500346
_______________________________________________________________________

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


Re: Any way to disable autowire on struts 2 actions?

Posted by Dave Newton <ne...@yahoo.com>.
--- On Mon, 10/13/08, Peter Butler wrote:
> From looking at the Struts 2 code and documentation I can't see any 
> way to disable this behaviour. I have tried setting 
> default-autowire="no" in my Spring application context configs and
> autowire="no" for the individual actions in the Spring file but these 
> settings have no effect.
> 
> Is there any way to test Struts 2 actions in isolation by
> populating dependencies using JMock objects, or am I stuck with
> Struts' insistence on autowiring everything?

Did you try one of the Spring plugin wiring settings?

http://struts.apache.org/2.0.11/docs/spring-plugin.html

Dave


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