<mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法

来源:互联网 发布:日本女孩长相知乎 编辑:程序博客网 时间:2024/06/05 23:41

      在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=""/>一起使用。


1 0