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

猜你喜欢

转载自www.cnblogs.com/trekxu/p/9123240.html
007
今日推荐