Spring 中context:annotation-config、context:component-scan和mvc:annotation-driven配置的作用
一、 <context:annotation-config/> 配置
<context:annotation-config />是为了**Spring所支持的一系列注解,诸如:Spring自带的@Required和@Autowired,JSR 250提供的 @PostConstruct, @PreDestroy and @Resource注解,JAX-WS提供的@WebServiceRef(如果可用),EJB 3的@EJB注解和JPA提供的@PersistenceContext 、@PersistenceUnit (如果可用)。
为什么在xml配置中加入这个配置就会自动使以上注解生效呢?
在Spring中这些注解的所提供的功能,是通过在Spring容器中注册一系列的BeanPostProcessor发挥强大的作用。例如:在 Spring 容器中注册AutowiredAnnotationBeanPostProcessor bean,就可以处理@Autowired注解。如果想使用@Resource 、@PostConstruct、@PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。如果想使用JPA提供的@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。
<context:annotation-config/>隐式地向 Spring容器注册这4个BeanPostProcessor:
AutowiredAnnotationBeanPostProcessor、
RequiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
即<context:annotation- config/>是用来使上述注解起作用的,也就是说**已经在application context中注册的bean。
之所以这样说是因为<context:annotation-config />仅能够在已经在已经注册过的bean上面起作用。
对于没有在spring容器中注册的bean,它并不能执行任何操作,也就是说如果你并没有spring容器中注册过bean(spring配置文件中配置bean就是注册(也就是说spring中不存在你想使用的bean)),那么上述的那些注解并不会在你未注册过的bean中起作用。
这里可能会有疑问,比如明明注册了AutowiredAnnotationBeanPostProcessorbean,那么岂不是可以随意使用@Autowired注解?
这里需要注意的是,@Autowired注解在你使用的时候自动注入的前提是,spring容器中已经有了该bean!
那么你可以随意在某个controller或者service使用该注解。并非该注解可以无中生有立即造一个bean给你使用!!
注意:<context:annotation-config /> 不能**事务类的注解,包括spring提供的@Transactional和EJB 3的@TransactionAttribute,请使用<tx:annotation-driven>。
下边是spring-context.xsd中关于<context:annotation-config />标签的文档说明:
二、<context:component-scan> 标签
<context:component-scan>做了<context:annotation-config>要做的事情,还额外支持@Component,@Repository,@Service,@Controller @RestController,@ControllerAdvice, 和 @Configuration注解。<context:component-scan>扫描base-package指定的包,将标注了上述注解的类自动注册为Spring bean。极大的简化了Spring的配置,这也时Spring 注解配置大行其道的原因所在。Spring boot也在此基础之上建立起来的。
所以配置<context:component-scan>就不需要配置<context:annotation- config/>!
下边是spring-context.xsd中关于<context:annotation-config />标签的文档说明:
三、mvc:annotation-driven 标签
这个标签以mvc作为命名空间,毫无疑问它是用在Spring MVC中的配置。用于配置注释驱动的Spring MVC Controller编程模型。有了这个配置,无需在xml配置中添加任何视图解析器和视图(比如:MappingJackson2JsonView),Spring会自动注册需要的ViewResolver和View。
对应的实现类是:org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser。
这个类主要是用来向Spring 容器中注册以下Bean:
RequestMappingHandlerMapping
BeanNameUrlHandlerMapping
RequestMappingHandlerAdapter
HttpRequestHandlerAdapter
SimpleControllerHandlerAdapter
ExceptionHandlerExceptionResolver
ResponseStatusExceptionResolver
DefaultHandlerExceptionResolver
前两个是HandlerMapping接口的实现类,用来处理请求映射的。
其中第一个是处理@RequestMapping注解的。
第二个会将controller类的名字映射为请求url(参考BeanNameViewResolver使用详解)。
中间三个是用来处理请求的。具体点说就是确定调用哪个controller的哪个方法来处理当前请求。
第一个处理@Controller注解的处理器,支持自定义方法参数和返回值(很酷)。
第二个是处理继承HttpRequestHandler的处理器。
第三个处理继承自Controller接口的处理器。
后面三个是用来处理异常的解析器。
另外还将提供以下支持:
① 支持使用ConversionService实例对表单参数进行类型转换;
② 支持使用@NumberFormatannotation、@DateTimeFormat注解完成数据类型的格式化;
③ 支持使用@Valid注解对Java bean实例进行JSR 303验证;
④ 支持使用@RequestBody和@ResponseBody注解
当需要Spring MVC提供JSON支持的时候,只要添加这个配置即可,无需手动在xml中配置
MappingJackson2JsonView bean。
参考文档:https://blog.****.net/j080624/article/details/60883328