Service injection into action

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.