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:
- Public class XKStrutsSpringObjectFactory extends SpringObjectFactory
- {
- /**
- *
- */
- Private static final long serialVersionUID = 1L;
- Private static final Logger LOG = LoggerFactory. getLogger (XKStrutsSpringObjectFactory. class );
-
-
- @ SuppressWarnings ("deprecation ")
- @ Inject
- Public XKStrutsSpringObjectFactory (@ Inject (value = StrutsConstants. STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE, required = false) String autoWire,
- @ Inject (value = StrutsConstants. STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE_ALWAYS_RESPECT, required = false) String alwaysAutoWire,
- @ Inject (value = StrutsConstants. STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE, required = false) String useClassCacheStr, @ Inject ServletContext servletContext,
- @ Inject (StrutsConstants. STRUTS_DEVMODE) String devMode, @ Inject Container container)
- {
-
- Super ();
- Boolean useClassCache = "true". equals (useClassCacheStr );
- If (LOG. isInfoEnabled ())
- {
- LOG.info ("Initializing Struts-Spring integration ...");
- }
-
- // Key code
- Object rootWebApplicationContext = Initialization. getInstance (). getApplicationContext ();
-
- If (rootWebApplicationContext instanceof RuntimeException)
- {
- RuntimeException runtimeException = (RuntimeException) rootWebApplicationContext;
- LOG. fatal (runtimeException. getMessage ());
- Return;
- }
-
- ApplicationContext appContext = (ApplicationContext) rootWebApplicationContext;
- If (appContext = null)
- {
- // Uh oh! Looks like the lifecycle listener wasn't installed. Let's
- // Inform the user
- String message = "*********** fatal error starting up struts-spring integration ********** \ n" + "Looks like spring listener was not configured for your web app! \ N"
- + "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext. \ n" + "You might need to add the following to web. xml: \ n" + "<listener> \ n"
- + "<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> \ n" + "</listener> ";
- LOG. fatal (message );
- Return;
- }
-
- String watchList = container. getInstance (String. class, "struts. class. reloading. watchList ");
- String acceptClasses = container. getInstance (String. class, "struts. class. reloading. acceptClasses ");
- String reloadConfig = container. getInstance (String. class, "struts. class. reloading. reloadConfig ");
-
- If ("true". equals (devMode) & StringUtils. isNotBlank (watchList) & appContext instanceof ClassReloadingXMLWebApplicationContext)
- {
- // Prevent class caching
- UseClassCache = false;
-
- ClassReloadingXMLWebApplicationContext reloadingContext = (ClassReloadingXMLWebApplicationContext) appContext;
- ReloadingContext. setupReloading (watchList. split (","), acceptClasses, servletContext, "true". equals (reloadConfig ));
- If (LOG. isInfoEnabled ())
- {
- LOG.info ("Class reloading is enabled. Make sure this is not used on a production environment! ", WatchList );
- }
-
- SetClassLoader (reloadingContext. getReloadingClassLoader ());
-
- // We need to reload the context, so our isntance of the factory is
- // Picked up
- ReloadingContext. refresh ();
- }
-
- This. setApplicationContext (appContext );
-
- Int type = AutowireCapableBeanFactory. AUTOWIRE_BY_NAME; // default
- If ("name". equals (autoWire ))
- {
- Type = AutowireCapableBeanFactory. AUTOWIRE_BY_NAME;
- }
- Else if ("type". equals (autoWire ))
- {
- Type = AutowireCapableBeanFactory. AUTOWIRE_BY_TYPE;
- }
- Else if ("auto". equals (autoWire ))
- {
- Type = AutowireCapableBeanFactory. AUTOWIRE_AUTODETECT;
- }
- Else if ("constructor". equals (autoWire ))
- {
- Type = AutowireCapableBeanFactory. AUTOWIRE_CONSTRUCTOR;
- }
- Else if ("no". equals (autoWire ))
- {
- Type = AutowireCapableBeanFactory. AUTOWIRE_NO;
- }
- This. setAutowireStrategy (type );
-
- This. setUseClassCache (useClassCache );
-
- This. setAlwaysRespectAutowireStrategy ("true". inclusignorecase (alwaysAutoWire ));
-
- If (LOG. isInfoEnabled ())
- {
- LOG.info ("... initialized Struts-Spring integration successfully ");
- }
- }
- }
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:
- <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!