在AbstractHandlerMethodMapping类中,获取HandlerMethods。
public void afterPropertiesSet() {
this.initHandlerMethods();
}
protected void initHandlerMethods() {
if(this.logger.isDebugEnabled()) {
this.logger.debug("Looking for request mappings in application context: " + this.getApplicationContext());
}
/*获取所有bean形成列表,后续将会遍历这些bean*/
String[] beanNames = this.detectHandlerMethodsInAncestorContexts?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];
/*通过isHandler函数,检查是否是Controller和RequestMapping注解。它是通过AnnotationUtils和ClassUtils静态类遍历的*/
if(!beanName.startsWith("scopedTarget.") && this.isHandler(this.getApplicationContext().getType(beanName))) {
this.detectHandlerMethods(beanName);
}
}
/*获取所有requestMapping对象完成注册的最后一步。*/
this.handlerMethodsInitialized(this.getHandlerMethods());
}
在该函数中检查局部变量
在AbstractHandlerMethodMapping的派生类中,即RequestMappingHandlerMapping中,实现了isHandler的类型判断。
在getHandlerMethods中
public Map getHandlerMethods() {
return Collections.unmodifiableMap(this.handlerMethods);
}
此外,附加findall代码。
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/findall")
public String handleList(Model model) {
logger.info("demo02 handleList");
List list = demoService.findAll();
model.addAttribute("results", list);
return "demo";
}
}
@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");
}
}