剖析<context:component-scan/>、<mvc:annotation-driven/>

来源:互联网 发布:nginx挂了怎么办 编辑:程序博客网 时间:2024/06/06 02:33

<context:component-scan/>和<mvc:annotation-driven/>


<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  
Java代码   收藏代码
  1. <context:component-scan base-package="com"></context:component-scan>  


web.xml  
Java代码   收藏代码
  1. <servlet>  
  2.     <servlet-name>mvc</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6. <servlet-mapping>  
  7.     <servlet-name>mvc</servlet-name>  
  8.     <url-pattern>*.do</url-pattern>  
  9. </servlet-mapping>  



后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即 / ,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了<mvc:annotation-driven/>之后,.do请求才又能被正确捕捉和处理。代码如下  
mvc-servlet.xml  
Java代码   收藏代码
  1. <context:component-scan base-package="com"></context:component-scan>  
  2. <mvc:annotation-driven/>  
  3. <mvc:resources mapping="/styles/**" location="/WEB-INF/resource/styles/"/>  
  4. <mvc:default-servlet-handler/>  


web.xml  
Java代码   收藏代码
  1. <servlet>  
  2.     <servlet-name>mvc</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6. <servlet-mapping>  
  7.     <servlet-name>mvc</servlet-name>  
  8.     <url-pattern>/</url-pattern>  
  9. </servlet-mapping>  


是什么原因造成这种区别的呢?为什么一开始没用<mvc:annotation-driven/>的时候可以,添加了默认servlet之后就不行了呢?

<context:annotation-config/>和<context:component-scan/>:

在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor

注册这4 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下

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

如果想使用@ Resource @ PostConstruct@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessorBean

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessorBean。同样,传统的声明方式如下:

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

一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。

   不过,呵呵,我们使用注解一般都会配置扫描包路径选项

<context:component-scan base-package=”XX.XX”/> 

    该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

原创粉丝点击