1. It's created when the Struts dispatcher filter receives a request
that matches an <action> element in the XML configuration file. Xwork
handles the creation, I believe (Struts internal DI framework). You're
running with Spring as the container, which mean Spring does the
action creation based on the name in the "class" element of the
<action> configuration - Spring matches this against a <bean> element
in the applicationContext.xml for your app.
2. When you create an object with "new", you're not creating it in a
container - "new" is dumb. Spring is smart - when Struts asks Spring
for an object (an action in this case), Spring can look at its XML and
say "hey, I see an @Autowired in that class Struts wanted. I'd better
work out what I need to inject in here". Spring manages the object
creation, and so can create the object, then inject. "new" just
creates it.
In this case, I'd guess you only have a single <bean> element in the
Spring configuration of type MyService. When Spring is asked to make
the object, and encounters the @Autowired annotation, it'll try to
find a <bean> element that matches (based on name, type, and probably
some other things I can't remember). In this case, it finds a <bean>
with type MyService. Then it uses some method to inject it into the
object Struts asked it for (I'm not 100% sure how it does it without
public setters, but it's pretty much the same end result as having a
public setter for MyService).
Think of Spring like a big object factory with instructions for how to
make objects. Struts asked for an object; Spring looked at its
blueprints and figured out how to put it together, then handed it to
Struts. That's basically what DI/IoC containers do.
I'd recommend you read the guides on springsource.org, or try a book
on Spring. I quite like Spring In Action, by Craig Walls & Ryan
Breidenbach (published by Manning, I think).
On 19 May 2009, at 15:58, Asleson, Ryan wrote:
Hello,
Not sure if this is a Struts 2 or Spring question but I'll start here.
I'm using Struts 2 along with Spring 2.5. In our Struts 2 Actions, we
use the @Autowired annotation to inject our service beans into the
Action class:
@Autowired
private MyService myService;
Note that we do *not* have a public setter method; just the @Autowired
annotation.
In our Spring config file, we have this tag which (supposedly) tells
Spring that autowiring will be used:
<context:annotation-config/>
When an Action is executed all of the @Autowired dependencies have
been
injected. There's a lot of magic going on there so I have some
questions:
1. How and when (and preferably, in what class) is the Action for a
request created?
2. How and when does Spring inject the @Autowired dependencies into
the
Action, especially considering there are no public setter methods for
the dependencies? If I create an Action using the "new" operator none
of the dependencies are injected, but somehow when Struts 2 creates
the
Action, the dependencies are injected. How or when does this happen?
How does Spring "know" what to inject and when to do it?
Thank you!!!
-Ryan
Ryan Asleson | Lead Developer
BI | Technology Solutions Group
www.biworldwide.com <http://www.biworldwide.com/>
Please consider the environment before printing.
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.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org