zoukankan      html  css  js  c++  java
  • component-scan和annotation-driven

    <context:component-scan/>

    该xml配置作用是启动Spring的组件扫描功能,自动扫描base-package指定的包及其子文件下的java文件,如果扫描到有@controller、@Service、@Repository、@Component等注解的java类,就会将这些类注册为bean。指定的包可以有多个,用分号隔开。
    如果指定了<context:component-scan/>就不用指定<context:annotation-config/>,前者包含后者。使用示例如下:
    <context:component-scan base-package="com.ouym.base" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    base-package指定需要扫描的包,include-filter指定需要扫描的注解,上述配置的意思是扫描com.ouym.base包下的@controller注解的java文件。

    而<context:annotation-config/>的实际作用是向spring容器注入以下几个类:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及RequiredAnnotationBeanPostProcessor 四个beanPostProcessor。这些类的作用主要是使一些注解生效,详细请自行查阅。


    <mvc:annotation-driver/>

    在spring中一般采用@RequestMapping注解来完成映射关系,要想使@RequestMapping注解生效必须向上下文中注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapter实例,这两个实例分别在类级别和方法级别处理。而annotation-driven配置帮助我们自动完成上述两个实例的注入。

    该注解要和扫描controller的注解一起放在spring主配置文件。
  • 相关阅读:
    osds have slow requests
    supervisor 管理 celery
    guacamole部署
    openstack IPV6
    修复VSAN无法看到主机磁盘
    kolla之docker私有仓库创建
    CSS日食与太阳碰撞
    vue-devtools必备工具
    VUE在BODY上绑定enter事件
    VUE输入框显示时自动聚焦
  • 原文地址:https://www.cnblogs.com/ouym/p/7654888.html
Copyright © 2011-2022 走看看