关于 ContextLoaderListener 和 DispatcherServlet 的加载问题

有A、B两个Controller
有C、D两个Service

Controller、Service均是用注解的方式写的,以context:component-scan的方式加载

A、C由ContextLoaderListener加载,B、D由DispatcherServlet加载

运行结果是Controller里只有B有效,访问A会报org.springframework.web.servlet.DispatcherServlet noHandlerFound,但是C、D两个Service都可以正常使用,在B中注入C、D时一切正常。(简单来说就是ContextLoaderListener中加载的Controller无效)

想请教以下问题:
1.都是Controller,为什么ContextLoaderListener加载的controller无效,DispatcherServlet加载controller有效?
2.都是ContextLoaderListener加载的bean,为什么Controller无效,但是别的bean可以正常使用?
3.这是我个人环境或设置的问题,还是spring本来就这么设计的?
4.有什么办法可以让ContextLoaderListener正常加载Controller吗?

麻烦尽量说的详细一点,谢谢。。。

在spring mvc中@Controller注解的bean必须由DispatcherServlet初始化的children webApplicationContext来管理,在DispatcherServlet初始化的context中会扫描当前容器所有的bean实例,根据类级别以及方法级别的映射信息注解组装成对应的HandleMappering信息,但是ContextLoaderListener是不具备这个功能的,所以你的A bean实例有问题。
另外,最好使用DispatcherServlet去扫描@Controller注解的bean,而servie bean使用COntextLoaderListener scan(web层controlller配置文件最好独立于其他业务bean配置文件)。

你可以这么理解,由DispatcherServlet加载相当于在ContextLoaderListener加载的基础上增加了controller的处理,所以DispatcherServlet加载的controller和bean都好用,ContextLoaderListener加载的bean好用而controller不好用.

我个人理解spring就是这么设计的,spring是个轻量的EJB框架,MVC只是其中的一块,这就是为什么需要把这个单独提出来由DispatcherServlet来处理,毕竟不是每一个工程都是WEB工程.