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.config.ModuleConfig; 29 import org.apache.struts.tiles.TilesRequestProcessor; 30 31 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 32 import org.springframework.context.ConfigurableApplicationContext; 33 import org.springframework.web.context.WebApplicationContext; 34 35 58 public class AutowiringTilesRequestProcessor extends TilesRequestProcessor { 59 60 private WebApplicationContext webApplicationContext; 61 62 private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_NO; 63 64 private boolean dependencyCheck = false; 65 66 67 public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException { 68 super.init(actionServlet, moduleConfig); 69 if (actionServlet != null) { 70 this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig); 71 this.autowireMode = initAutowireMode(actionServlet, moduleConfig); 72 this.dependencyCheck = initDependencyCheck(actionServlet, moduleConfig); 73 } 74 } 75 76 87 protected WebApplicationContext initWebApplicationContext( 88 ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException { 89 90 WebApplicationContext wac = 91 DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig); 92 if (wac instanceof ConfigurableApplicationContext) { 93 ((ConfigurableApplicationContext) wac).getBeanFactory().ignoreDependencyType(ActionServlet.class); 94 } 95 return wac; 96 } 97 98 110 protected int initAutowireMode(ActionServlet actionServlet, ModuleConfig moduleConfig) { 111 return DelegatingActionUtils.getAutowireMode(actionServlet); 112 } 113 114 124 protected boolean initDependencyCheck(ActionServlet actionServlet, ModuleConfig moduleConfig) { 125 return DelegatingActionUtils.getDependencyCheck(actionServlet); 126 } 127 128 129 132 protected final WebApplicationContext getWebApplicationContext() { 133 return this.webApplicationContext; 134 } 135 136 139 protected final int getAutowireMode() { 140 return autowireMode; 141 } 142 143 146 protected final boolean getDependencyCheck() { 147 return dependencyCheck; 148 } 149 150 151 155 protected Action processActionCreate( 156 HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) 157 throws IOException { 158 159 Action action = super.processActionCreate(request, response, mapping); 160 getWebApplicationContext().getAutowireCapableBeanFactory().autowireBeanProperties( 161 action, getAutowireMode(), getDependencyCheck()); 162 return action; 163 } 164 165 } 166 | Popular Tags |