Integrating Spring in Struts2

Source: Internet
Author: User
Tags constant constructor

The official definition of spring is: a lightweight IOC and AOP container framework that uses a technology called dependency injection. The so-called dependency injection means that the responsibility for cooperation between the creation object and the protocol dependent object is transferred from the object itself to the "factory", which is usually provided by the IOC container.

The integration of STRUTS2 with spring uses the Spring plug-in package, which is published with Struts2. The spring plug-in enhances the creation of core framework objects by overriding (override) Struts2 objectfactory. When an object is created, it is associated with the id attribute in the spring configuration file with the class attribute in the Struts2 configuration file, which, if found, is created by spring, or created by the STRUTS2 framework itself, and then assembled by spring. The spring plug-in has several specific roles to follow:

1. Allow spring to create action, interceptror, and result

2. Objects created by struts can be assembled by spring

3. 2 interceptors are provided to automatically assemble the action, if spring objectfactory is not used

The thing to note here is that we don't have to register the action in spring, although we can do that, usually the struts framework automatically creates the action object from the action mapping.

There are several steps to implementing the integration with struts and spring

1. Include the Struts2-spring-plugin-2.0.8.jar file in our application and place it under the Web-inf/lib directory. There is a struts-plugin.xml file in this plug-in package, which reads as follows:

<struts>
<bean type= "com.opensymphony.xwork2.ObjectFactory" Name= "Spring" class= " Org.apache.struts2.spring.StrutsSpringObjectFactory "/>
<constant name=" struts.objectfactory "value=" Spring "/>
<package name=" Spring-default
<interceptors>
<interceptor name= " Autowiring "class=" Com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor/>
< Interceptor Name= "sessionautowiring" class= " Org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor "/>
</interceptors>
</package>
</struts>

Here it overwrites the frame constant struts.objectfactory and sets it to "Spring", which is actually used for abbreviations, We can write full name: Org.apache.struts2.spring.StrutsSpringObjectFactory. This abbreviated "Spring" is corresponding to the name attribute in the bean configuration. By default, all objects created by the framework are instantiated by Objectfactory, and Objectfactory provides an integrated approach to other IOC containers such as spring, Pico, and so on. A class that overrides this objectfactory must inherit the Objectfactory class or any of its subclasses, with a constructor without parameters. Here we use Org.apache.struts2.spring.StrutsSpringObjectFactory instead of the default objectfactory.

In addition, as we said above, if the action is not created using spring Objectfactory, the plug-in provides two interceptors to assemble the action automatically, and by default the framework uses an automatic assembly policy that is name, That is, the framework will go to spring to look for the same bean with the action attribute name, and the optional assembly strategy is: type, auto, constructor, We can set it by constant Struts.objectFactory.spring.autoWire.

2. Configure the spring listener to include the Spring.jar package in the application

Insert the following sentence into the Web.xml file, and place the Spring.jar file under the Web-inf/lib directory.

<listener>
<listener-class> Org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>

3. To register an object with the spring configuration file

The following steps are the same as we used to use spring alone, and now we have completed the integration of struts and spring. One thing to note here is that we also specify the location of the spring configuration file, by default, the container will go under the Web-inf directory to find the Applicationcontext.xml file. If we want to specify a configuration file elsewhere or specify more than one profile, you can specify it by defining the Context-param element in the Web.xml file, as follows:

<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml,classpath:applicationContext-*.xml </param-value> </context-param>

The preceding paragraph indicates that the Applicationcontext.xml file below Web-inf and all files that match the applicationcontext-*.xml pattern below classpath are loaded as spring configuration files.

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.