SpringMVC中配置的区分点context:annotation-config,context:component-scan,mvc:annotation-driven

本文详细解析了SpringMVC中的xml配置,包括<context:annotation-config/>、<context:component-scan/>和<mvc:annotation-driven/>的作用及区别,阐述了它们如何支持不同注解的使用。

SpringMVC的xml配置细节点

在springMVC配置中,一些配置用于处理对应的注解,以下为几点比较难以区分的配置:

  1. <context:annotation-config />
  2. <context:component-scan base-package=“com.xx.xx” />
  3. <mvc:annotation-driven />



<context:annotation-config />
  1. 作用

    在Spring中配置**<context:annotation-config />**,它的作用是隐式的向Spring容器中注册以下几个处理器:AutowiredAnnotationBeanPostProcessor

    CommonAnnotationBeanPostProcessor

    PersistenceAnnotationBeanPostProcessor

    RequiredAnnotationBeanPostProcessor

  2. AutowiredAnnotationBeanPostProcessor

    此处理器主要是为**@Autowired**注解进行注册,如果需要用该注解就需要注册

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  1. CommonAnnotationBeanPostProcessor

    此处理器主要是为**@PersistenceContext**注解进行注册,如果需要用该注解就需要注册

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  1. PersistenceAnnotationBeanPostProcessor

    此处理器主要是为**@Required**注解进行注册,如果需要用该注解就需要注册

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  1. RequiredAnnotationBeanPostProcessor

    此处理器主要是为**@Resource、@ PostConstruct、@ PreDestroy等**注解进行注册,如果需要用该注解就需要注册

<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>

<context:annotation-config /> 只能在已经注册过的bean起作用,也就是说,上述那些注解无法使用在未曾注册过的bean中。



<context:component-scan base-package=“com.xx.xx” />
  1. 作用

    该配置的作用是让启动bean定义注解,也就是说,对在base-package指定的包中扫描在类上定义的注解,这样就不用传统的bean进行注册。

  2. 注意点

    注意:<context:component-scan base-package=“com.xx.xx” />不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了

  3. 支持的注解

    1. @Autowired
    2. @Component
    3. @Repository
    4. @Service
    5. @Controller
    6. @RestController,
    7. @ControllerAdvice,
    8. @Configuration



<mvc:annotation-driven />
  1. 作用

    用于启动@Controller注解

  2. 注意点

    使用spring mvc中的@Controller注解,就必须要配置<mvc:annotation-driven />,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值