1 16 17 package org.springframework.web.struts; 18 19 import java.io.IOException ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.struts.action.Action; 26 import org.apache.struts.action.ActionMapping; 27 import org.apache.struts.action.ActionServlet; 28 import org.apache.struts.action.RequestProcessor; 29 import org.apache.struts.config.ModuleConfig; 30 31 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 32 import org.springframework.context.ConfigurableApplicationContext; 33 import org.springframework.web.context.WebApplicationContext; 34 35 76 public class AutowiringRequestProcessor extends RequestProcessor { 77 78 private WebApplicationContext webApplicationContext; 79 80 private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_NO; 81 82 private boolean dependencyCheck = false; 83 84 85 public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException { 86 super.init(actionServlet, moduleConfig); 87 if (actionServlet != null) { 88 this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig); 89 this.autowireMode = initAutowireMode(actionServlet, moduleConfig); 90 this.dependencyCheck = initDependencyCheck(actionServlet, moduleConfig); 91 } 92 } 93 94 105 protected WebApplicationContext initWebApplicationContext( 106 ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException { 107 108 WebApplicationContext wac = 109 DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig); 110 if (wac instanceof ConfigurableApplicationContext) { 111 ((ConfigurableApplicationContext) wac).getBeanFactory().ignoreDependencyType(ActionServlet.class); 112 } 113 return wac; 114 } 115 116 128 protected int initAutowireMode(ActionServlet actionServlet, ModuleConfig moduleConfig) { 129 return DelegatingActionUtils.getAutowireMode(actionServlet); 130 } 131 132 142 protected boolean initDependencyCheck(ActionServlet actionServlet, ModuleConfig moduleConfig) { 143 return DelegatingActionUtils.getDependencyCheck(actionServlet); 144 } 145 146 147 150 protected final WebApplicationContext getWebApplicationContext() { 151 return this.webApplicationContext; 152 } 153 154 157 protected final int getAutowireMode() { 158 return autowireMode; 159 } 160 161 164 protected final boolean getDependencyCheck() { 165 return dependencyCheck; 166 } 167 168 169 173 protected Action processActionCreate( 174 HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) 175 throws IOException { 176 177 Action action = super.processActionCreate(request, response, mapping); 178 getWebApplicationContext().getAutowireCapableBeanFactory().autowireBeanProperties( 179 action, getAutowireMode(), getDependencyCheck()); 180 return action; 181 } 182 183 } 184 | Popular Tags |