Integrate automatic assembly of struts2, spring, and spring

Source: Internet
Author: User

Integrate automatic assembly of struts2, spring, and spring

1. The required jar files are the jar files required by the spring and struts2 frameworks.AndJar files, such as commons-logging.jarAnd so on.In additionAlso requiredStruts2-spring-plugin-x.xx.jar in the struts2 release package.Ii. Integration Process:(1) Add the corresponding configuration of webapplicationcontext in Web. xml. The following two configuration methods are essentially the same. 1. the listener can be used for servlet 2.3 and later versions. The configuration is as follows: <context-param> <param-Name> contextconfiglocation </param-Name> <param-value>/WEB-INF/classes/applicationcontext. XML </param-value> </context-param> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> If the spring configuration file is named applicationcontext. XML, and placed in the WEB-INF directory, you do not need to configure <context-param> because the contextloaderlistener is found under the WEB-INF directory by default The file named applicationcontext. xml. To be placed in the WEB-INF/classes/directory, or if there are multiple spring configuration files, the above configuration is required and listed in <param-value>, separated by commas. 2. <listener> is not supported in servlet versions earlier than 2.3. You need to configure <servlet> in the following format: <context-param> <param-Name> contextconfiglocation </param-Name> <param-value>/WEB-INF/classes/applicationcontext. XML </param-value> </context-param> <servlet-Name> contextloaderservlet </servlet-Name> <servlet-class> Org. springframework. web. context. contextloaderservlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> because the servlet configuration only To enable the contextloaderservlet to work when the container starts, you do not need to reference the servlet, so you do not need to configure <servlet-mapping>. (2)After loading webapplicationcontext in Web. XML, spring and struts2 can be implemented..There are two methods for integration:1. First implementation method:1) configure the struts business logic controller class in the spring configuration file, and inject the business class referenced in the business logic controller. Note,The business logic controller class must be configuredScope = "prototype"!
Example: <bean id = "loginaction" class = "yaso. struts. action. loginaction "> <property name =" logindao "ref =" logindao "/> </bean> 2) in struts. when configuring action in XML or equivalent struts2 configuration file, specify the class attribute of <action> as the ID or name value of the bean in the spring configuration file. Example: <action name = "loginaction" class ="Loginaction"> <Result name =" success ">/index. jsp </result> </Action>2. Method 2:1) The business class is configured in the spring configuration file, and the business logic controller class does not need to be configured. The struts2 action is configured as before integrating spring, the class attribute of <action> specifies the full qualified name of the business logic controller class. 2) The business class referenced in the business logic controller class does not need to be initialized by itself. The spring plug-in of struts2 will use Bean's automatic assembly to inject the business class, in fact, the business logic controller is not created by struts2, but by the spring plug-in of struts2. By default, the plug-in uses the by name Method for assembly. You can add the struts2 constant to modify the matching method: struts. objectfactory. spring. autowire = typename. The optional Assembly parameters are as follows:)Name: Equivalent to autowire = "byname" in spring configuration. This isMissing Value.B)Type: It is equivalent to autowire = "bytype" in spring configuration ". C)Auto: It is equivalent to autowire = "autodetect" in spring configuration ". D)Constructor: It is equivalent to autowire = "constructor" in spring configuration ". 4. If Multiple object factory is used in struts2, you must explicitly specify the object factory through the struts2 constant by using the following method: struts. objectfactory = spring; if no multiple object factory is used, this step can be omitted. 5. You can add struts2 constants to specify whether to use spring's class cache mechanism. The value can be set to true or false. The default value is true. The setting method is struts. objectfactory. Spring. useclasscache = false. 6. So far, the integration of the two methods has been completed. The two integration methods are essentially the same. The difference is that,When the second automatic assembly method is usedIt is difficult to configure the business logic controller in spring. 

Spring autowire is automatically assembled in applications. We often use the <ref> label to inject the dependent objects to the JavaBean. However, for a large system, this operation will consume a lot of resources, and we have to spend a lot of time and energy to create and maintain the <ref> label in the system. Spring provides an automatic assembly Mechanism for us. When defining beans, the <bean> tag has an autowire attribute, we can specify it to allow containers to automatically inject dependency objects to managed JavaBean. The autowire attribute of <bean> has the following values:
1. No: automatic assembly is disabled. The default value of autowire.
2. byname: Find and inject the object on which the JavaBean depends using the attribute name. For example, if the computer class has a printer attribute and Its autowire attribute is set to byname, the Spring IoC container will find the bean whose ID/name attribute is printer in the configuration file, then, use the seter method to inject it.
3. bytype: Find and inject the objects on which the JavaBean depends based on the attribute type. For example, if the class computer has a printer attribute whose type is printer, after specifying its autowire attribute as bytype, the Spring IoC container will find the bean whose class attribute is printer, use the seter Method for injection.
4. constructor: similar to bytype, it also finds dependent objects by type. The difference between bytype and bytype is that it uses constructor instead of seter injection.
5. autodetect: select an automatic injection mode between bytype and constructor.
6. Default: the default-autowire attribute of the upper-level label <beans> is determined.

Note: When bean is configured, The autowire attribute in the <bean> tag has a higher priority than its parent tag, that is, if the default-autowire attribute is defined as byname in the parent tag, when <bean> is defined as bytype, the Spring IoC container uses the <bean> label configuration first.

The following example shows how to use automatic assembly in an application (for project code, see routine 3.2 ). Create a Java project, add spring development capabilities to it, and create an IOC. create the computer, host, and dispaly respectively, and add the host and display attributes of the host type for the computer class, add another run method so that the computer can "run" together. The Property Code is as follows:

1 package IOC. test;
2
3 /***//**
4 * @ author zhangyong
5 */
6 public class computer {
7
8 private host;
9 private display;
10 // computer running Method
11 Public void run (){
12 system. Out. println ("Hello, I'm a computer, running! ");
13 system. Out. Print ("" + host. Run () + ",");
14 system. Out. println (display. Run ());
15}
16 // The geter and seter methods are omitted.
17}
18

1 package IOC. test;
2
3 Public class host {
4 Public String run (){
5 return "I Am a host, running! ";
6}
7}
8

1 package IOC. test;
2
3 Public class display {
4 Public String run (){
5 return "I'm a monitor, running! ";
6}
7}
8. Modify the spring configuration file to enable the IOC container to automatically assemble "host" and "display" for our "computer ". Configure two beans, host and Display respectively. Configure a bean named computer1 with the autowire attribute set as byname. Similarly, computer1 and computer3 are configured as the bytype and default attributes respectively, finally, set the default-autowire attribute of the <beans> label to autodetect. Now, the configuration has been completed. We can see that the computer bean is not explicitly injected with the dependent objects host and display. The configuration code is as follows: 1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <beans default-autowire = "autodetect">
3 <bean id = "computer1" class = "IOC. Test. Computer" autowire = "byname"> </bean>
4 <bean id = "computer2" class = "IOC. Test. Computer" autowire = "bytype"> </bean>
5 <bean id = "computer3" class = "IOC. Test. Computer" autowire = "default"> </bean>
6
7 <bean id = "host" class = "IOC. Test. Host"> </bean>
8 <bean id = "display" class = "IOC. Test. display"> </bean>
9 </beans>
10 now we can create a test class to test whether the bean we need is automatically assembled for us during spring. 1 package IOC. test;
2
3 // import omitted
4 public class testmain {
5
6 public static void main (string [] ARGs ){
7 applicationcontext AC = new classpathxmlapplicationcontext (
8 "applicationcontext. xml ");
9 // byname
10 computer computer1 = (Computer) AC. getbean ("computer1 ");
11 system. Out. println ("autowire =/" byname /":");
12 computer1.run ();
13
14 // bytype
15 computer computer2 = (Computer) AC. getbean ("computer2 ");
16 system. Out. println ("autowire =/" bytype /":");
17 computer2.run ();
18
19 // default
20 Computer computer3 = (Computer) AC. getbean ("computer3 ");
21 system. Out. println ("autowire =/" default /":");
22 computer3.run ();
23}
24}
25

 

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.