zoukankan      html  css  js  c++  java
  • 007 标记注解@Controller

    一 .概述

      作为spring标准的四个基础组件标示注解,@Controller变成了在springmvc中控制器的标示注解,到底是怎么实现的呢?

      这个是我们需要了解的一个问题,本节,从源码上说一下@Controller注解在springmvc之中的作用.


    二 .注解的结构  

    /** 含有该注解的类的隐含的意思就是该类应该是一个控制器.
     * Indicates that an annotated class is a "Controller" (e.g. a web controller).
     * 表示该注解的类可以被扫描器从classpath之中加载并注册,另外一个作用就是
      完成处理器方法的绑定 * <p>This annotation serves as a specialization of {@link Component @Component}, * allowing for implementation classes to be autodetected through classpath scanning. * It is typically used in combination with annotated handler methods based on the * {@link org.springframework.web.bind.annotation.RequestMapping} annotation. *
    @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Controller { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty String otherwise) */ String value() default ""; }

    我们从文档之中,可以看到@Controller的一个特色作用就是完成处理器方法的绑定工作.


    三 .@Controller的运行源码  

        protected boolean isHandler(Class<?> beanType) {
            return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
                    AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
        }

    我们在RequestMappingHandlerMapping之中看到了这一段代码,

      它会判断这个Bean是否是含有一个注解,如果有Controller,就是一个控制器.

      如果不是,含有@RequestMapping也是一个控制器.


    四 .总结

      我们现在知道了,@Controller为什么比其它的注解拥有更强的功能了

  • 相关阅读:
    EF Code First列名 'Discriminator' 无效的问题
    并行编程
    通过http上下文判断是否是Ajax请求
    桌面或文件夹里单击鼠标右键新建菜单下不显示文本文档的处理方法
    Frameset框架集的应用
    定时帧(基于定时器的动画 11.1)
    自定义缓冲函数(缓冲 10.2)
    动画速度(缓冲 10.1)
    手动动画(9.2 图层时间)
    CAMediaTiming`协议(9.1 图层时间)
  • 原文地址:https://www.cnblogs.com/trekxu/p/9123240.html
Copyright © 2011-2022 走看看