请教<context:component-scan/>和<mvc:annotation-driven/>的区别
来源:互联网 发布:黑光灯诱捕法估算法 编辑:程序博客网 时间:2024/06/03 11:36
转自:http://www.iteye.com/problems/66133
自己看了官方文档,也到网上查了下,目前理解如下:
<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。
<context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。
一开始我在写配置的时候,只写了<context:component-scan/>,并没有使用<mvc:annotation-driven/>,servlet拦截*.do,.do请求可以被正确捕捉和处理。代码如下
mvc-servlet.xml
- <context:component-scan base-package="com"></context:component-scan>
web.xml
- <servlet>
- <servlet-name>mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了<mvc:annotation-driven/>之后,.do请求才又能被正确捕捉和处理。代码如下
mvc-servlet.xml
- <context:component-scan base-package="com"></context:component-scan>
- <mvc:annotation-driven/>
- <mvc:resources mapping="/styles/**" location="/WEB-INF/resource/styles/"/>
- <mvc:default-servlet-handler/>
web.xml
- <servlet>
- <servlet-name>mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
看到此篇文章,说明了上面标红的内容:
http://blog.csdn.net/u013516966/article/details/42611913
在spring中<context:annotation-config/>表示在自动装配时,我们可以使用注解的方式 例如:@Autowired,但是此时在xml文件中仍需要bean配置,只不过此时代替了bean的property
<context:component-scan base-package=""/>表示扫描指定的包,将注解的类注册为spring容器中的bean,那么此时我们在xml文件中就不需要定义bean,实现xml文件的零配置
<mvc:annotation-driven/>表示在SpringMVC中注册了DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,表示为@controller、@requestMapping做好准备工作,但是 此时被注解的bean并没有被加入到spring容器中,此时需要用<context:component-scan base-package=""/>扫描并注册bean,因此我们在SpringMVC中出现将<mvc:annotation-driven/>、<context:component-scan base-package=""/>一起使用。
- 请教<context:component-scan/>和<mvc:annotation-driven/>的区别
- <context:component-scan/>和<mvc:annotation-driven/>的区别
- <context:component-scan/>和<mvc:annotation-driven/>的区别
- <context:component-scan/> <mvc:annotation-driven /> 区别
- <context:component-scan/>和<mvc:annotation-driven/>区别
- <context:annotation-config/>,<mvc:annotation-driven/>和<context:component-scan>之间的关系
- 剖析<context:component-scan/>、<mvc:annotation-driven/>
- <context:component-scan/> <mvc:annotation-driven />
- spring mvc 中mvc:annotation-driven和context:component-scan区别
- <mvc:annotation-driven/>,<context:annotation-config/>,<context:component-scan/>详解
- <mvc:annotation-driven/> <context:annotation-config/> <context:component-scan>
- Spring MVC: <context:annotation-config> 和 <context:component-scan>区别
- mvc:annotation-scan和context:component-scan
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- [spring-mvc] <context:component-scan/> <mvc:annotation-driven/>
- <context:component-scan>扫描@Component @Controller@Service注解的的类 <mvc:annotation-driven />启用注解
- (6)Mysql运算符和函数
- 自己实现一个简单的布谷鸟散列
- 在一个线程加锁,另一个线程解锁
- 2、quartz程序实例
- Web框架的本质
- 请教<context:component-scan/>和<mvc:annotation-driven/>的区别
- Python实现标准的Kmeans算法
- Android应用开发之所有动画使用详解
- SpringMvc+POI 处理Excel的导入操作(一般form提交和jquery.form.js提交)
- Tensorflow安装-Anacoda
- 算法-->级数
- 学习SPI的一些疑惑
- Tablayout+Fragment+侧拉 一些简单的实现
- Tree Reconstruction UVA