1 16 17 package org.springframework.web.servlet.handler; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.springframework.beans.BeansException; 23 import org.springframework.beans.factory.BeanFactoryUtils; 24 import org.springframework.context.ApplicationContextException; 25 import org.springframework.util.StringUtils; 26 27 53 public class BeanNameUrlHandlerMapping extends AbstractUrlHandlerMapping { 54 55 private boolean detectHandlersInAncestorContexts = false; 56 57 58 67 public void setDetectHandlersInAncestorContexts(boolean detectHandlersInAncestorContexts) { 68 this.detectHandlersInAncestorContexts = detectHandlersInAncestorContexts; 69 } 70 71 72 76 public void initApplicationContext() throws ApplicationContextException { 77 super.initApplicationContext(); 78 detectHandlers(); 79 } 80 81 87 protected void detectHandlers() throws BeansException { 88 if (logger.isDebugEnabled()) { 89 logger.debug("Looking for URL mappings in application context: " + getApplicationContext()); 90 } 91 String [] beanNames = (this.detectHandlersInAncestorContexts ? 92 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object .class) : 93 getApplicationContext().getBeanNamesForType(Object .class)); 94 95 for (int i = 0; i < beanNames.length; i++) { 97 String beanName = beanNames[i]; 98 String [] urls = determineUrlsForHandler(beanName); 99 if (urls.length > 0) { 100 registerHandler(urls, beanName); 102 } 103 else { 104 if (logger.isDebugEnabled()) { 105 logger.debug("Rejected bean name '" + beanNames[i] + "': no URL paths identified"); 106 } 107 } 108 } 109 } 110 111 117 protected String [] determineUrlsForHandler(String beanName) { 118 List urls = new ArrayList (); 119 if (beanName.startsWith("/")) { 120 urls.add(beanName); 121 } 122 String [] aliases = getApplicationContext().getAliases(beanName); 123 for (int j = 0; j < aliases.length; j++) { 124 if (aliases[j].startsWith("/")) { 125 urls.add(aliases[j]); 126 } 127 } 128 return StringUtils.toStringArray(urls); 129 } 130 131 } 132 | Popular Tags |