[ 
https://issues.apache.org/jira/browse/WW-3257?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18115865#comment-18115865
 ] 

Lukasz Lenart commented on WW-3257:
-----------------------------------

Verified against current main with a probe test in the Spring plugin. This 
works as designed, so closing as Not A Problem.

The Spring plugin resolves the action's class attribute against a Spring bean 
id. When there is no bean with that id, Spring still creates the instance (via 
createBean, honouring the configured autowire strategy) and autowires it. That 
was already the behaviour in the 2.1.x code, so the controllers were created by 
Spring; what did not happen was picking up a bean definition registered under a 
different id with explicit property wiring. With the Convention and REST 
plugins the class attribute is always the fully qualified class name, so such a 
definition is never matched. A lookup by type instead would risk handing a 
singleton-scoped bean to Struts as a per-request action, so it is deliberately 
not done.

Three supported ways to get the dependencies injected into an auto-configured 
controller, all confirmed by the probe:

Register the bean (or a Spring alias for it) under the fully qualified class 
name of the controller, with prototype scope. The class-name lookup then finds 
the Spring definition, including its explicit property wiring.

Rely on autowiring: name the setter after the bean id (the default by-name 
strategy) or set the autowire strategy to type.

Use Spring's Autowired annotation on the controller together with 
annotation-config; the createBean path runs the annotation post-processors.

The Spring plugin documentation describes the class-to-id contract but does not 
spell out the fully-qualified-name alias for auto-discovered actions; that is a 
documentation gap, not a code change.

> Get Controller bean from Spring
> -------------------------------
>
>                 Key: WW-3257
>                 URL: https://issues.apache.org/jira/browse/WW-3257
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Plugin - REST, Plugin - Spring
>    Affects Versions: 2.1.6
>            Reporter: Antonius Ng
>            Priority: Minor
>             Fix For: 7.4.0
>
>
> Because of the automatic configuration of actions, when using 
> StrutsSpringObjectFactory, the Controller objects are not created by Spring, 
> thus the dependencies of the Controller are not injected.
> The workaround that I did was to write a new Class that extends 
> StrutsSpringObjectFactory and override the method buildBean(Class clazz, 
> Map<String, Object> extraContext) to get the Controller bean from appContext 
> if the clazz is the Controller class.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to