struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Denis Cabasson <denis.cabas...@gmail.com>
Subject Re: spring, struts2, convention plugin, how to "wire" an action class
Date Fri, 14 May 2010 00:42:55 GMT
My best guess is that the Action instance used in the web application is 
not actually loaded from the configuration in your applicationContext, 
but directly by Struts 2 (through the Spring Factory, but still Struts 2).

That mean that you will have a bean named myAction in you application 
context, but that is not the bean that will actually be used by Struts 
to serve your request. Struts will create another instance, which won't 
have any of your configuration. This different instance is then 
autowired by type (defaut behaviour) which means your service 
(configured in your context) gets injected because it has the right type.

I can't actually confirm that behaviour, but that seems like the likely 
explanation. I will be able to double check tomorrow.

To check that, I can recommend 2 things :
* Set the autowiring to be by name, create 2 beans named verbose and 
message, and see how that works
* Leaving the autowiring by type, create 2 instances of your service, 
and my best guess is that Struts will crash, unable to decide between the 2

I think what your are missing is the fact that while struts uses Spring 
to manage the lifecycle of its action, it doesn't use the action defined 
in your application context, but will load its own instances, and 
autowire them.

Denis.

Le 2010-05-13 20:18, Jake Vang a écrit :
> roger,
>
> thanks for the steps. i went through each of those steps and made sure
> my settings/configurations complied. however, i still cannot
> initialize Action classes using Spring.
>
> as an illustration of the problem i am facing, i have created an
> example that you may download and verify for yourself that this does
> not work.
>
> http://www.box.net/shared/iycamr9uo6
>
> this will download a zip file with an Eclipse project; unzip it. if
> you have Ant, you simply type in "ant war" and the war file will be
> created. if you have Tomcat, then drop the war file into the webapps
> directory (assuming you have Tomcat 6.0).
>
> if you go to http://localhost:8080/myapp/, you will see four lines displayed.
> String from Action: default
> boolean from Action: false
> String from Service: overwritten
> boolean from Service: true
>
> The first and second lines confirm that the String and boolean values
> are not injected into the Action class. The third and fourth lines
> confirm that the String and boolean values are injected into the
> service class.
>
> thanks.
>
> On Thu, May 13, 2010 at 9:32 AM, RogerV<roger.varley@googlemail.com>  wrote:
>    
>>
>>
>> Jake Vang wrote:
>>      
>>>        
>>>> If you want Spring to create your action class (as opposed to Struts
>>>> creating them) then you need to define your action in the
>>>> applicationContext.xml file.
>>>>          
>>> how do you do that? here's a couple of ways i have tried that do NOT work.
>>>
>>>        
>> 1) Add<constant name="struts.objectFactory"
>> value="org.apache.struts2.spring.StrutsSpringObjectFactory" />  to your
>> struts.xml file.
>>
>> 2) Add<listener>
>>
>> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>>     </listener>
>>
>> to your web.xml
>>
>> 3) Make sure that you have the Struts2 Spring plugin installed.
>>
>> 4) Put something along the lines of
>> <bean id="myAction" class="com.somwhere.mypackage.MyAction"
>> scope="prototype"/>  (The scope prototype is important otherwise Spring
>> creates a singleton rather than a new action on each invocation which is
>> what Struts 2 expects)
>>
>> and that should get Spring instantiating your action classes for you.
>>
>> Although I'm guessing from your response you've already got to that stage.
>>
>> I'm not sure that I understand what you are trying to acheive with
>>
>> <bean name="/action" class="com.services.MyAction">
>>   <property name="propertyA" value="${propertyA}"/>
>> </bean>
>>
>> If you're trying to inject another class, then the format is on the lines of
>>
>> <bean name="/action" class="com.services.MyAction">
>>   <property name="classA" ref="classA"/>
>> </bean>
>>
>> where you need a setter that matches the name parameter (setClassA(ClassA
>> classA)) in MyAction. The ref value points to another<bean id="classA"
>> class="com.services.classA"/>
>>
>> HTH and I haven't completely misunderstood what you are trying to do.
>>
>> Regards
>>
>>
>> --
>> View this message in context: http://old.nabble.com/spring%2C-struts2%2C-convention-plugin%2C-how-to-%22wire%22-an-action-class-tp28545412p28547552.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> 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
>
>
>    


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


Mime
View raw message