Service injection into action
In the past, every time I tried to get the service, I obtained it through the getbean of webapplicationcontext in action. In spring, I only configured this layer of service and didn't configure the action. Today, it's boring to configure the action to spring, which turns out to be so simple.
1. Add jar package struts2-spring-plugin-xxx.jar
2. Add the following in the Struts. xml configuration file:
<constant name="struts.objectFactory" value="spring"/>
3. Add the required action to spring. xml.
<bean class="com.gavin.sma.action.UserInfoAction" id="userInfoAction"> <property name="userInfoService" ref="userInfoService"></property> </bean>
Okay, it's done.
By the way objectfactory (reprinted to http://aixiangct.blog.163.com/blog/static/9152246120101016113237982)
Objectfactory, Is a very important class in xwork, isGenerate action. When xwork is used separately, actions are created in this class.
Struts2 encapsulates objectfactory and a strutsobjectfactory, which is a class inherited from objectfactory. Therefore, struts2 uses strutsobjectfactory instead of the objectfactory of xwork to generate action by default.
Struts2 plug-in struts2-spring-plugin-2.1.8.1.jar, made an object factory, strutsspringobjectfactory, its parent class is xwork springobjectfactory, the most primitive parent class is objectfactory, I think the purpose of xwork's springobjectfactory is to inherit this class and implement the spring factory.
So in struts2, there are three object factories,
Org. Apache. struts2.spring. strutsspringobjectfactory (provided by the spring plug-in of struts2)
Org. Apache. struts2.impl. strutsobjectfactory (comes with struts2)
Com. opensymphony. xwork2.objectfactory (comes with xwork ).
They can all be used to generate actions.
Objectfactory is used by default in xwork, and strutsobjectfactory is used by default in struts2. The strutsspringobjectfactory is used by default when the spring plug-in of struts2 is used. These are all automatic, and the first two are the content of struts2. Both
<Constant name = "struts. objectfactory" value = "Spring"/>
Here, the value is
<Bean type = "com. opensymphony. xwork2.objectfactory" name = "Spring" class = "org. Apache. struts2.spring. strutsspringobjectfactory"/>
Which factory is defined in the bean, and which factory is used by struts2.
Service injection into action