spring <context:component-scan />及<mvc:annotation-driven /> <context:annotation-

 

Since spring version 2.5, annotation injection has been supported. Using annotations can save a lot of configuration in xml and make the configuration file more concise. In our spring configuration files, we often encounter <context:component-scan />such <mvc:annotation-driven />configurations. What do these two configurations help us do?

1.<context:component-scan />
  • 1
  • 2

Starting from the spring 2.5 version, a powerful component scanning function is provided. It can automatically scan, detect and instantiate your beans from the classpath. When we add <context:component-scan />this xml element to the spring configuration file, it means to start the component scanning function of spring. Specify the attribute base-package (package name), which means that the java files under the base-package package or sub-packages will be scanned. If the java classes annotated with @controller, @Service, @Repository, @Component, etc. are scanned, these will be scanned. Classes are registered as beans. You can also use semicolons to separate multiple scan packets. 
If it is configured in the configuration file <context:component-scan />, there is no need to configure <context:annotation-config/>it, because the former already includes the latter. <context:annotation-config/>The function is to inject four beanPostProcessors, AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor and RequiredAnnotationBeanPostProcessor, into the spring container. If you want the @Autowired annotation to take effect and be able to be injected automatically, you must declare the AutowiredAnnotationBeanPostProcessor to the spring container, like this:

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

Correspondingly, if you want to use annotations such as @Resource, @PostConstruct, @PreDestroy, etc., you must declare CommonAnnotationBeanPostProcessor. 
If you want to use @PersistenceContext you have to declare PersistenceAnnotationBeanPostProcessor. 
If you want to declare RequiredAnnotationBeanPostProcessor with @Required annotation.

Generally, these annotations are often used. For example, @Autowired and @Resource are often used for automatic injection. There are also @PostConstruct and @PreDestroy annotations, which indicate the methods that are called when the bean is initialized and destroyed. We see that if you want to use these annotations, it is too cumbersome to declare the configuration one by one in the traditional way, and the readability becomes worse after more configurations. Help us inject 4 bean post processors, which greatly simplifies our configuration.

2.<mvc:annotation-driven />
  • 1
  • 2

In spring MVC, you <context:component-scan />can start component scanning to instantiate beans and complete automatic injection. However, URL requests are mapped to controller classes and their corresponding handler methods, which requires more declarations from the web application. Generally, we use the @RequestMapping annotation to complete the mapping relationship, and we must also register DefaultAnnotationHandlerMapping and an AnnotationMethodHandlerAdapter instance in the context. These two instances handle @RequestMapping annotations at the class level and method level, respectively. It looks like this:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean   class="org.springframework.web.servlet.mvc.annotation. AnnotationMeth odHandlerAdapter"/>

<context:component-scan base-package="" />
  • 1
  • 2
  • 3
  • 4
  • 5

<mvc:annotation-driven/>配置能够帮我们省去DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter的声明配置。同时,还提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。其中对json的处理是我们项目中经常用到的,使用Jackson库将对象序列化为json字符串。所以我们的项目中,也必须含有jackson的jar包。当我们返回的数据不是html标签的页面时,而是其他格式的数据时(如xml、json等),我们使用@ResponseBody注解,将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

 

 

========================================================

现在常用框架中SpringMVC.xml配置是:

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

那么<context:annotation-config/>呢?


首先看一下三个注解各自定义:

① <context:annotation-config/>

1.如果你想使用@Autowired注解,那么就必须事先在 spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

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

3.如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

4.如果想使用 @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就是注册),那么上述的那些注解并不会在你未注册过的bean中起作用。


<context:component-scan>

<context:component-scan>做了<context:annotation-config>要做的事情,还额外支持@Component,@Repository,@Service,@Controller注解。
并且<context:component-scan>扫描base-package并且在application context中注册扫描的beans.

所以配置<context:component-scan>就不需要配置<context:annotation- config/>


③ <mvc:annotation-driven/>

至于该项看前缀就应该知道是springmvc所需要的注解。

<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。

我们找到对应的实现类是:

org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser。

通过阅读类注释文档,我们发现这个类主要是用来向工厂中注册了

  • RequestMappingHandlerMapping
  • BeanNameUrlHandlerMapping
  • RequestMappingHandlerAdapter
  • HttpRequestHandlerAdapter
  • SimpleControllerHandlerAdapter
  • ExceptionHandlerExceptionResolver
  • ResponseStatusExceptionResolver
  • DefaultHandlerExceptionResolver

上面几个Bean实例。这几个类都是用来做什么的呢?

前两个是HandlerMapping接口的实现类,用来处理请求映射的。

  • 其中第一个是处理@RequestMapping注解的。
  • 第二个会将controller类的名字映射为请求url。

中间三个是用来处理请求的。具体点说就是确定调用哪个controller的哪个方法来处理当前请求。

  • 第一个处理@Controller注解的处理器,支持自定义方法参数和返回值(很酷)。
  • 第二个是处理继承HttpRequestHandler的处理器。
  • 第三个处理继承自Controller接口的处理器。

后面三个是用来处理异常的解析器。

另外还将提供以下支持:

① 支持使用ConversionService实例对表单参数进行类型转换; 
② 支持使用@NumberFormatannotation、@DateTimeFormat注解完成数据类型的格式化; 
③ 支持使用@Valid注解对Java bean实例进行JSR 303验证; 
④ 支持使用@RequestBody和@ResponseBody注解

 

转自:http://blog.csdn.net/sunhuwh/article/details/25558867

<annotaion-driven/>标签:

这个标签对应的实现类是org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

仔细阅读它的注释文档可以很明显的看到这个类的作用。解析这个文档:

这个类主要注册8个类的实例:

1.RequestMappingHandlerMapping

2.BeanNameUrlHandlerMapping

3.RequestMappingHandlerAdapter

4.HttpRequestHandlerAdapter

5.SimpleControllerHandlerAdapter

6.ExceptionHandlerExceptionResolver

7.ResponseStatusExceptionResolver

8.DefaultHandlerExceptionResolver

1是处理@RequestMapping注解的,2.将controller类的名字映射为请求url。1和2都实现了HandlerMapping接口,用来处理请求映射。

3是处理@Controller注解的控制器类,4是处理继承HttpRequestHandlerAdapter类的控制器类,5.处理继承SimpleControllerHandlerAdapter类的控制器。所以这三个是用来处理请求的。具体点说就是确定调用哪个controller的哪个方法来处理当前请求。

6,7,8全部继承AbstractHandlerExceptionResolver,这个类实现HandlerExceptionResolver,该接口定义:接口实现的对象可以解决处理器映射、执行期间抛出的异常,还有错误的视图。

所以<annotaion-driven/>标签主要是用来帮助我们处理请求映射,决定是哪个controller的哪个方法来处理当前请求,异常处理。

 

<context:component-scan/>标签:

它的实现类是org.springframework.context.annotation.ComponentScanBeanDefinitionParser.

把鼠标放在context:component-scan上就可以知道有什么作用的,用来扫描该包内被@Repository @Service @Controller的注解类,然后注册到工厂中。并且context:component-scan激活@ required。@ resource,@ autowired、@PostConstruct @PreDestroy @PersistenceContext @PersistenceUnit。使得在适用该bean的时候用@Autowired就行了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262767&siteId=291194637