BeanNameUrlHandlerMapping遍历Controller注解过程

1.关键运行栈细节

2.关注AbstractDetectingUrlHandlerMapping类的detectHandlers函数。

protected void detectHandlers() throws BeansException {
        if(this.logger.isDebugEnabled()) {
            this.logger.debug("Looking for URL mappings in application context: " + this.getApplicationContext());
        }
        /*获取所有bean的名字,然后遍历测试*/
        String[] beanNames = this.detectHandlersInAncestorContexts?BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.getApplicationContext(), Object.class):this.getApplicationContext().getBeanNamesForType(Object.class);
        String[] var2 = beanNames;
        int var3 = beanNames.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String beanName = var2[var4];
            /*将检验beanName的名字,当名字是以反斜扛开头的,均当作是一个URL处理器*/
            String[] urls = this.determineUrlsForHandler(beanName);
            if(!ObjectUtils.isEmpty(urls)) {
                this.registerHandler(urls, beanName);
            } else if(this.logger.isDebugEnabled()) {
                this.logger.debug("Rejected bean name \'" + beanName + "\': no URL paths identified");
            }
        }
    }

3.上图局部变量截图

4.附上determineUrlsForHandler的相关代码。

protected String[] determineUrlsForHandler(String beanName) {
        ArrayList urls = new ArrayList();
        if(beanName.startsWith("/")) {
            urls.add(beanName);
        }

        String[] aliases = this.getApplicationContext().getAliases(beanName);
        String[] var4 = aliases;
        int var5 = aliases.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String alias = var4[var6];
            if(alias.startsWith("/")) {
                urls.add(alias);
            }
        }

        return StringUtils.toStringArray(urls);
    }

5.附相关代码

@Controller("/demo_servlet2")
public class Demo2Servlet extends HttpServlet {	
	@PostConstruct  //init-method="init"
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		logger.info("Demo2Servlet init start");		
		
		logger.info("Demo2Servlet init end");
	}

	@RequestMapping("/findall")
	public String handleList(Model model) {
		logger.info("demo02  handleList");
		return "demo";
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		logger.info("Demo2Servlet service start");

		response.setContentType(CONTENT_TYPE);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		String result = demoService.getById(1l).getName();
		//Integer.valueOf(result); //测试异常显示页面
		PrintWriter out = response.getWriter();
		out.println("");
		out.println("\n");
		out.println("" + result + "\n");
		out.println("\n");

		out.flush();
		out.close();

		logger.info("Demo2Servlet service end");
	}

	@PreDestroy //destroy-method="destroy"
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		logger.info("Demo2Servlet destroy start");
		
		logger.info("Demo2Servlet destroy end");
	}
	
}