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为什么比其它的注解拥有更强的功能了

  • 相关阅读:
    Java基础----ArrayList中的clear方法以及ArrayList对象
    LeetCode152:乘积最大子数组
    LeetCode18:四数之和
    LeetCode120 :三角形最小路径和
    LeetCode406:根据身高重建队列
    LeetCode347:前 K 个高频元素
    LeetCode-146:LRU缓存机制
    LeetCode-17:电话号码的字母组合
    LeetCode
    任务调度冲突
  • 原文地址:https://www.cnblogs.com/trekxu/p/9123240.html
Copyright © 2011-2022 走看看