In-depth integration of S2S3H4 framework (2) deep integration of Spring

Source: Internet
Author: User

 

In the previous article, I wrote the key issues in struts2. As for the detailed integration process, I will not elaborate on them. Except for the key issues described by me, everything else is the same. Next, let's talk about Spring integration. Most of the articles searched on the Internet introduce the integration of spring and struts2. They all share the same routine. They will throw spring configurations to the src root directory and then go to the web. configure a listener in xml and add struts in the struts2 configuration. objectFactory description. That's all done. Although it's easy to use, it's all just like a gourd. I don't know why. In fact, since struts has this description, it means that all instances in struts2 must be obtained through the ObjectFactory defined in this definition, the method used in the factory to obtain objects is integrated with anything. Therefore, the key to integration with spring is not web. xml configuration, or even web. xml listeners are not allowed at all. The following is a practical example: Compile XKStrutsSpringObjectFactory to inherit SpringObjectFactory. The Code is as follows:

 
 
  1. Public class XKStrutsSpringObjectFactory extends SpringObjectFactory
  2. {
  3. /**
  4. *
  5. */
  6. Private static final long serialVersionUID = 1L;
  7. Private static final Logger LOG = LoggerFactory. getLogger (XKStrutsSpringObjectFactory. class );
  8.  
  9.  
  10. @ SuppressWarnings ("deprecation ")
  11. @ Inject
  12. Public XKStrutsSpringObjectFactory (@ Inject (value = StrutsConstants. STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE, required = false) String autoWire,
  13. @ Inject (value = StrutsConstants. STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE_ALWAYS_RESPECT, required = false) String alwaysAutoWire,
  14. @ Inject (value = StrutsConstants. STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE, required = false) String useClassCacheStr, @ Inject ServletContext servletContext,
  15. @ Inject (StrutsConstants. STRUTS_DEVMODE) String devMode, @ Inject Container container)
  16. {
  17.  
  18. Super ();
  19. Boolean useClassCache = "true". equals (useClassCacheStr );
  20. If (LOG. isInfoEnabled ())
  21. {
  22. LOG.info ("Initializing Struts-Spring integration ...");
  23. }
  24.  
  25. // Key code
  26. Object rootWebApplicationContext = Initialization. getInstance (). getApplicationContext ();
  27.  
  28. If (rootWebApplicationContext instanceof RuntimeException)
  29. {
  30. RuntimeException runtimeException = (RuntimeException) rootWebApplicationContext;
  31. LOG. fatal (runtimeException. getMessage ());
  32. Return;
  33. }
  34.  
  35. ApplicationContext appContext = (ApplicationContext) rootWebApplicationContext;
  36. If (appContext = null)
  37. {
  38. // Uh oh! Looks like the lifecycle listener wasn't installed. Let's
  39. // Inform the user
  40. String message = "*********** fatal error starting up struts-spring integration ********** \ n" + "Looks like spring listener was not configured for your web app! \ N"
  41. + "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext. \ n" + "You might need to add the following to web. xml: \ n" + "<listener> \ n"
  42. + "<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> \ n" + "</listener> ";
  43. LOG. fatal (message );
  44. Return;
  45. }
  46.  
  47. String watchList = container. getInstance (String. class, "struts. class. reloading. watchList ");
  48. String acceptClasses = container. getInstance (String. class, "struts. class. reloading. acceptClasses ");
  49. String reloadConfig = container. getInstance (String. class, "struts. class. reloading. reloadConfig ");
  50.  
  51. If ("true". equals (devMode) & StringUtils. isNotBlank (watchList) & appContext instanceof ClassReloadingXMLWebApplicationContext)
  52. {
  53. // Prevent class caching
  54. UseClassCache = false;
  55.  
  56. ClassReloadingXMLWebApplicationContext reloadingContext = (ClassReloadingXMLWebApplicationContext) appContext;
  57. ReloadingContext. setupReloading (watchList. split (","), acceptClasses, servletContext, "true". equals (reloadConfig ));
  58. If (LOG. isInfoEnabled ())
  59. {
  60. LOG.info ("Class reloading is enabled. Make sure this is not used on a production environment! ", WatchList );
  61. }
  62.  
  63. SetClassLoader (reloadingContext. getReloadingClassLoader ());
  64.  
  65. // We need to reload the context, so our isntance of the factory is
  66. // Picked up
  67. ReloadingContext. refresh ();
  68. }
  69.  
  70. This. setApplicationContext (appContext );
  71.  
  72. Int type = AutowireCapableBeanFactory. AUTOWIRE_BY_NAME; // default
  73. If ("name". equals (autoWire ))
  74. {
  75. Type = AutowireCapableBeanFactory. AUTOWIRE_BY_NAME;
  76. }
  77. Else if ("type". equals (autoWire ))
  78. {
  79. Type = AutowireCapableBeanFactory. AUTOWIRE_BY_TYPE;
  80. }
  81. Else if ("auto". equals (autoWire ))
  82. {
  83. Type = AutowireCapableBeanFactory. AUTOWIRE_AUTODETECT;
  84. }
  85. Else if ("constructor". equals (autoWire ))
  86. {
  87. Type = AutowireCapableBeanFactory. AUTOWIRE_CONSTRUCTOR;
  88. }
  89. Else if ("no". equals (autoWire ))
  90. {
  91. Type = AutowireCapableBeanFactory. AUTOWIRE_NO;
  92. }
  93. This. setAutowireStrategy (type );
  94.  
  95. This. setUseClassCache (useClassCache );
  96.  
  97. This. setAlwaysRespectAutowireStrategy ("true". inclusignorecase (alwaysAutoWire ));
  98.  
  99. If (LOG. isInfoEnabled ())
  100. {
  101. LOG.info ("... initialized Struts-Spring integration successfully ");
  102. }
  103. }
  104. }

The key step lies in the 26th lines. You only need to use your own method to obtain the applicationContext object. Spring also provides multiple implementations for the ApplicationContext object. Since we want to define the name and location of the configuration, we can use FileSystemXmlApplicationContext. Well, I will not go into details about the usage of this class. It's easy to pass in the configuration path during instantiation. Finally, in the struts configuration, we will apply our own factory applications, such:

 
 
  1. <constant name="struts.objectFactory" value="com.xk.commons.config.XKStrutsSpringObjectFactory" /> 

Note that the ClassReloadingXMLWebApplicationContext class is used in line 71 of the Code. The FilesystemAlterationListener interface is implemented in the parent class of this class, this interface is not found in the jar package provided by struts and spring, which is included in the jci library of apache commons, specific to the jar package for the commons-jci-fam-1.0.jar: http://commons.apache.org/jci/downloads.html.

This article is from "My Joy is my joy ~" Blog. For more information, contact the author!

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.