背景
作为一名前端开发,在直接学习springboot时,注解看着一头雾水,像个黑盒子,所以今天来学习注解的源码分析
@Controller
spring mvn 注解
@Controller("customController")
public class BasicController {
}
源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
别名给Component.class
@AliasFor(
annotation = Component.class
)
注解方法,实际上定义了注解的属性
String value() default "";
}
@AliasFor
自定义注解
@RequestMapping // 应用 @RequestMapping 注解
public @interface WebEndpoint {
@AliasFor(annotation = RequestMapping.class, attribute = "path")
String value() default "/";
}
这里面有几个关键点
-
@interface
关键字用于定义一个注解(Annotation)。注解是一种特殊类型的接口 -
当@Controller 注解没有提供值给 value 属性。这意味着 value 属性将使用其默认值,即空字符串。在这种情况下,Spring 框架通常会根据类名(MyController)生成一个
默认的 Bean 名称
(通常是将首字母小写
,即 myController),则定义了 Spring Bean 的名称。 -
@AliasFor注解为 Component.class,但没有明确指定 attribute。因此,根据 Spring 的规则,此时 value() 属性在 @Controller 注解中将会作为 @Component 注解中 value() 属性的别名。这意味着当你
设置 @Controller 的 value 属性时,实际上也就设置了 @Component 的 value 属性。
后续补充。。。