1 16 17 package org.springframework.web.servlet.handler.metadata; 18 19 import org.springframework.beans.BeansException; 20 import org.springframework.beans.factory.BeanInitializationException; 21 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 22 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 23 import org.springframework.context.ConfigurableApplicationContext; 24 import org.springframework.core.Constants; 25 import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; 26 27 49 public abstract class AbstractPathMapHandlerMapping extends AbstractUrlHandlerMapping { 50 51 52 private static final Constants constants = new Constants(AutowireCapableBeanFactory.class); 53 54 private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT; 55 56 private boolean dependencyCheck = true; 57 58 59 70 public void setAutowireModeName(String constantName) throws IllegalArgumentException { 71 setAutowireMode(constants.asNumber(constantName).intValue()); 72 } 73 74 86 public void setAutowireMode(int autowireMode) { 87 this.autowireMode = autowireMode; 88 } 89 90 95 public void setDependencyCheck(boolean dependencyCheck) { 96 this.dependencyCheck = dependencyCheck; 97 } 98 99 100 105 public void initApplicationContext() throws BeansException { 106 super.initApplicationContext(); 107 108 if (!(getApplicationContext() instanceof ConfigurableApplicationContext)) { 109 throw new IllegalStateException ( 110 "[" + getClass().getName() + "] needs to run in a ConfigurableApplicationContext"); 111 } 112 ConfigurableListableBeanFactory beanFactory = 113 ((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory(); 114 detectAndCreateHandlers(beanFactory); 115 } 116 117 129 protected void detectAndCreateHandlers(ConfigurableListableBeanFactory beanFactory) throws BeansException { 130 try { 131 Class [] handlerClasses = getClassesWithPathMapAttributes(); 132 if (logger.isDebugEnabled()) { 133 logger.debug("Found " + handlerClasses.length + " attribute-targeted handlers"); 134 } 135 136 for (int i = 0; i < handlerClasses.length; i++) { 138 Class handlerClass = handlerClasses[i]; 139 140 Object handler = beanFactory.createBean(handlerClass, this.autowireMode, this.dependencyCheck); 144 145 beanFactory.registerSingleton(handlerClass.getName(), handler); 150 151 PathMap[] pathMaps = getPathMapAttributes(handlerClass); 153 registerHandler(pathMaps, handler); 154 } 155 } 156 catch (BeansException ex) { 157 throw ex; 158 } 159 catch (Exception ex) { 160 throw new BeanInitializationException("Could not retrieve PathMap attributes", ex); 161 } 162 } 163 164 171 protected void registerHandler(PathMap[] pathMaps, Object handler) throws BeansException, IllegalStateException { 172 for (int j = 0; j < pathMaps.length; j++) { 173 PathMap pathMap = pathMaps[j]; 174 String path = pathMap.getUrl(); 175 if (!path.startsWith("/")) { 176 path = "/" + path; 177 } 178 registerHandler(path, handler); 179 } 180 } 181 182 183 188 protected abstract Class [] getClassesWithPathMapAttributes() throws Exception ; 189 190 197 protected abstract PathMap[] getPathMapAttributes(Class handlerClass) throws Exception ; 198 199 } 200 | Popular Tags |