SpringFrameworkとStruts1.3の連携
SpringFrameworkとStruts1.3の連携に関して記載しようと思います。
SpringとStrutsを連携する場合は、通常は、リクエストプロセッサを追加ます。
Springで、AutowiringRequestProcessorクラスが提供されているので、それを使えば、StrutsのActionクラスへBeanをインジェクションできますが、
Struts1.3には、Chain-of-responsibilityパターンが使えるようになっています。
HTTPリクエストに対して、順番になんらかの処理を実行することができます。
その為には、リクエストプロセッサのComposableRequestProcessorクラスを使う必要があります。
それにより、ActionクラスへBeanをインジェクションする部分を自作し、
StrutsのChain-of-responsibilityパターンの機能を利用し、その中へ組み込む方法をとる必要があります。
StrutsのChainへ組みこむActionへBeanをインジェクションするクラスを下記の様に実装し対処しました。
import org.apache.struts.action.Action; import org.apache.struts.action.ActionServlet; import org.apache.struts.chain.commands.ActionCommandBase; import org.apache.struts.chain.contexts.ActionContext; import org.apache.struts.chain.contexts.ServletActionContext; import org.apache.struts.config.ModuleConfig; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.struts.DelegatingActionUtils; @SuppressWarnings("deprecation") public class ActionAutowiring extends ActionCommandBase { private Object lock = new Object(); private WebApplicationContext webApplicationContext; private int autowire; private boolean dependency; public boolean execute(ActionContext context) throws Exception { Action action = context.getAction(); if (action != null) { getWebApplicationContext(context). getAutowireCapableBeanFactory(). autowireBeanProperties(action, autowire, dependency); } return false; } protected WebApplicationContext getWebApplicationContext(ActionContext context) throws Exception { if (webApplicationContext == null) { synchronized(lock) { ServletActionContext servletActionContext = (ServletActionContext)context; ActionServlet actionServlet = servletActionContext.getActionServlet(); ModuleConfig moduleConfig = context.getModuleConfig(); webApplicationContext = DelegatingActionUtils. findRequiredWebApplicationContext(actionServlet, moduleConfig); if (webApplicationContext instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext)webApplicationContext). getBeanFactory(). ignoreDependencyType(ActionServlet.class); } autowire = DelegatingActionUtils.getAutowireMode(actionServlet); dependency = DelegatingActionUtils.getDependencyCheck(actionServlet); } } return webApplicationContext; } }
StrutsのChainを使う為の設定が必要なので、その設定を行いたいと思います。
web.xmlとstruts-config.xmlに必要な設定をしていきます。
web.xmlに関しては、SpringとStrutsの設定をします。
設定内容は、下記の設定になっています。
<?xml version="1.0" encoding="UTF-8"?> <web-app id="chronos" version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/config/struts/struts-config.xml</param-value> </init-param> <init-param> <param-name>spring.autowire</param-name> <param-value>byName</param-value> </init-param> <init-param> <param-name>chainConfig</param-name> <param-value>/WEB-INF/config/struts/chain-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
次にstruts-config.xmlには、リクエストプロセッサとして、ComposableRequestProcessorを使うように設定します。
設定内容は、下記の様に設定します。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> ..... <controller processorClass="org.apache.struts.chain.ComposableRequestProcessor"/> </struts-config>
これで、StrutsとSpringの基本的な設定が完了したので、Chainの設定をします。
Chainは、chain-config.xmlで、定義します。
定義の内容に関しては、下記の様な定義になります。
<?xml version="1.0" encoding="UTF-8"?> <catalog name="struts"> <define name= "lookup" className= "org.apache.commons.chain.generic.LookupCommand"/> <chain name="servlet-standard"> <command className="org.apache.struts.chain.commands.ExceptionCatcher" catalogName="struts" exceptionCommand="servlet-exception"/> <lookup catalogName="struts" name="process-action" optional="false"/> <lookup catalogName="struts" name="process-view" optional="false"/> </chain> <chain name="process-action"> <lookup catalogName="struts" name="servlet-standard-preprocess" optional="true"/> <command className="org.apache.struts.chain.commands.servlet.SelectLocale"/> <command className="org.apache.struts.chain.commands.servlet.SetOriginalURI"/> <command className="org.apache.struts.chain.commands.servlet.RequestNoCache"/> <command className="org.apache.struts.chain.commands.servlet.SetContentType"/> <command className="org.apache.struts.chain.commands.servlet.SelectAction"/> <command className="org.apache.struts.chain.commands.servlet.AuthorizeAction"/> <command className="org.apache.struts.chain.commands.CreateActionForm"/> <command className="org.apache.struts.chain.commands.servlet.PopulateActionForm"/> <command className="org.apache.struts.chain.commands.servlet.ValidateActionForm"/> <command className="org.apache.struts.chain.commands.servlet.SelectInput"/> <command className="org.apache.struts.chain.commands.ExecuteCommand"/> <command className="org.apache.struts.chain.commands.servlet.SelectForward"/> <command className="org.apache.struts.chain.commands.SelectInclude"/> <command className="org.apache.struts.chain.commands.servlet.PerformInclude"/> <command className="org.apache.struts.chain.commands.servlet.CreateAction"/> <command className="jp.co.chronos.devel.system.base.web.struts.ActionAutowiring"/> <command className="org.apache.struts.chain.commands.servlet.ExecuteAction"/> </chain> <chain name="process-view"> <command className="org.apache.struts.chain.commands.ExecuteForwardCommand"/> <command className="org.apache.struts.chain.commands.servlet.PerformForward"/> </chain> <chain name="servlet-exception"> <command className="org.apache.struts.chain.commands.servlet.ExceptionHandler"/> <command className="org.apache.struts.chain.commands.servlet.PerformForward"/> </chain> </catalog>
これで、リクエストプロセッサのComposableRequestProcessorクラスを使い、
先ほど作ったクラスをChainに組み込み、ActionクラスへBeanをインジェクション可能になります。