context:annotation-config与mvc:annotation-driven和context:component-scan区别

本文深入探讨了SpringMVC框架中三个关键配置标签的功能与用途:context:annotation-config、context:component-scan与mvc:annotation-driven。解释了它们如何在Spring容器中激活注解、注册组件以及提供MVC框架所需的支持。重点阐述了如何通过这些配置优化应用的依赖注入和自动化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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


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

① <context:annotation-config/>

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

这里写图片描述


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


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

JPA中你将会遇到这个注解。


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就是注册(也就是说spring中不存在你想使用的bean)),那么上述的那些注解并不会在你未注册过的bean中起作用。


这里可能会有疑问,比如明明注册了AutowiredAnnotationBeanPostProcessorbean,那么岂不是可以随意使用@Autowired注解?

这里需要注意的是,@Autowired注解在你使用的时候自动注入的前提是,spring容器中已经有了该bean!

那么你可以随意在某个controller或者service使用该注解。并非该注解可以无中生有立即造一个bean给你使用!!


<context:component-scan>

<context:component-scan>做了<context:annotation-config>要做的事情,还额外支持@Component,@Repository,@Service, 
@Controller @RestController, @ControllerAdvice, and @Configuration 注解。

并且<context:component-scan>扫描base-package并且在application context中注册扫描到的使用注解注入的beans。

这里着重注意第二段话,<context:component-scan>该注解可以扫描并注册你使用注解诸如@controller @service @component..的bean!!!

也就是说,你不用xml中显示配置,需要的时候尽管用@Resource或者@Autowired来自动注入!!!

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

该标签的XSD说明见下图:

这里写图片描述


③ <mvc:annotation-driven/>

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

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

org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

源码示例如下:

/**
 * A {@link BeanDefinitionParser} that provides the configuration for the
 * {@code <annotation-driven/>} MVC namespace element.
 *
 * <p>This class registers the following {@link HandlerMapping}s:</p>
 * <ul>
 * <li>{@link RequestMappingHandlerMapping}
 * ordered at 0 for mapping requests to annotated controller methods.
 * <li>{@link BeanNameUrlHandlerMapping}
 * ordered at 2 to map URL paths to controller bean names.
 * </ul>
 *
 * <p><strong>Note:</strong> Additional HandlerMappings may be registered
 * as a result of using the {@code <view-controller>} or the
 * {@code <resources>} MVC namespace elements.
 *
 * <p>This class registers the following {@link HandlerAdapter}s:
 * <ul>
 * <li>{@link RequestMappingHandlerAdapter}
 * for processing requests with annotated controller methods.
 * <li>{@link HttpRequestHandlerAdapter}
 * for processing requests with {@link HttpRequestHandler}s.
 * <li>{@link SimpleControllerHandlerAdapter}
 * for processing requests with interface-based {@link Controller}s.
 * </ul>
 *
 * <p>This class registers the following {@link HandlerExceptionResolver}s:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
 * through @{@link ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
 * with @{@link ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
 * exception types
 * </ul>
 *
 * <p>This class registers an {@link org.springframework.util.AntPathMatcher}
 * and a {@link org.springframework.web.util.UrlPathHelper} to be used by:
 * <ul>
 * <li>the {@link RequestMappingHandlerMapping},
 * <li>the {@link HandlerMapping} for ViewControllers
 * <li>and the {@link HandlerMapping} for serving resources
 * </ul>
 * Note that those beans can be configured by using the {@code path-matching} MVC namespace element.
 *
 * <p>Both the {@link RequestMappingHandlerAdapter} and the
 * {@link ExceptionHandlerExceptionResolver} are configured with instances of
 * the following by default:
 * <ul>
 * <li>A {@link ContentNegotiationManager}
 * <li>A {@link DefaultFormattingConversionService}
 * <li>A {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}
 * if a JSR-303 implementation is available on the classpath
 * <li>A range of {@link HttpMessageConverter}s depending on what 3rd party
 * libraries are available on the classpath.
 * </ul>
 *
 * @author Keith Donald
 * @author Juergen Hoeller
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 * @author Brian Clozel
 * @author Agim Emruli
 * @since 3.0
 */
class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {

    public static final String HANDLER_MAPPING_BEAN_NAME = RequestMappingHandlerMapping.class.getName();

    public static final String HANDLER_ADAPTER_BEAN_NAME = RequestMappingHandlerAdapter.class.getName();

    public static final String CONTENT_NEGOTIATION_MANAGER_BEAN_NAME = "mvcContentNegotiationManager";
  •  

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

  • 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注解

实验十二 SpringMVC 一、实验目的 1、掌握SpringMVC项目构建 2、掌握SpringMVC数据接收传递的原理 二、实验过程 1、配置SpringMVC开发环境 a)创建新的项目,TestSpringMvc b)为项目添加Web框架SpringMVC框架 c)添加classes目录 d)添加javaee依赖 e)添加lib输出配置 将依赖文件添加至lib中 f)修改web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> <filter> <filter-name>charactorEncoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>charactorEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> g)修改applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <context:annotation-config></context:annotation-config> <context:component-scan base-package="cs18.controller"/> </beans> h)添加controller,TestController.java @Controller public class TestController { @RequestMapping(value = "/showName") public String showName(HttpServletRequest request, HttpServletResponse response) { //访问传递的参数 ; return "index.jsp"; } } i)配置tomcat,添加项目部署 j)启动tomcat,在浏览器中输入以下地址 http://localhost:8080/TestSpringMvc/showName?name=fredy 2、测试以下不同类型数据绑定方法 a)HttpServletRequet,HttpSession,Model/ModelMap b)简单数据类型绑定(int,String等) c)POJO数据绑定 d)包装POJO数据绑定 e)自定义数据类型绑定 f)数组或集合绑定 g)使用Json风格类型的数据绑定
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值