springMVC的三种注解生效方式对比

· <context:annotation-config/>

在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<context:annotation-config/>这样一条配置,它的作用是隐式的向Spring容器注册

AutowiredAnnotationBeanPostProcessor,

CommonAnnotationBeanPostProcessor,

PersistenceAnnotationBeanPostProcessor,

RequiredAnnotationBeanPostProcessor 

 这4个BeanPostProcessor.注册这4个bean处理器主要的作用是为了你的系统能够识别相应的注解。                        
 例如:

1.    如果想使用@Autowired注解,需要在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean。传统的声明方式:<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

2.    如果想使用@PersistenceContext注解,需要在Spring容器中声明PersistenceAnnotationBeanPostProcessor Bean。传统的声明:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

3.    如果想使用@Required注解,需要在Spring容器中声明RequiredAnnotationBeanPostProcessor Bean。传统声明方式:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

4. 如果想使用@Resource、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。传统申明方式:

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

所以,如果按照传统声明一条一条去声明注解Bean,就会显得十分繁琐。因此如果在Spring的配置文件中事先加上<context:annotation-config/>这样一条配置的话,那么所有注解的传统声明就可以被 忽略,即不用在写传统的声明,Spring会自动完成声明。

· <context:component-scanbase-package="com.xx.xx"/> 

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

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

 <context:component-scan/>的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

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

 

 @Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。注解之后就不需要set/get方法了。

注意:如果有多个配置文件,在最顶层的配置文件(启动类所在的配置文件)中加入<context:component-scan base-package="com.xx.xx" /> 。(如controller层的配置文件中)

· <mvc:annotation-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的哪个方法来处理当前请求,异常处理。

结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值