spring-boot的main函数可以不放在启动类里面

在我前面的文章spring-boot自动装配写道,spring-boot启动时根据启动函数中设置的启动类,然后根据启动类上的注解@SpringBootApplication 开启整个项目的自动注入过程。

前几天,我研究@SpringBootApplication注解,发现其是被@Inherited注解标记的。@Inherited是做什么的呢?根据jdk文档我们可以知道,@Inherited主要是为了实现子类继承父类的注解这一功能。它的特性如下:

1、没有被@Inherited注解标记的注解,不具有继承特性,在子类中获取不到父类的注解;
2、注解继承,它只作用于子类与父类之间的继承,对于接口之间的继承和类与接口之间的实现,这两种继承关系不起作用;
 3、注解标记的注解,在使用时,如果父类和子类都使用的注解是同一个,那么子类的注解会覆盖父类的注解。

简单的测试下:

定义注解@Test1

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Test1 {
}

定义类Test2并打上@Test1注解

@Test1
public class Test2 {
}

定义类Test2继承Test1

public class Test3 extends Test2 {
}

测试:

Annotation[] annotations = Test3.class.getAnnotations();
 Annotation[] annotations1 = Test2.class.getAnnotations();
 System.out.println(Arrays.stream(annotations).map(annotation -> annotation.toString()).collect(Collectors.joining(",")));
 System.out.println(Arrays.stream(annotations1).map(annotation -> annotation.toString()).collect(Collectors.joining(",")));

测试结果如下:

@com.cdd.springboot.demo.Test1()
@com.cdd.springboot.demo.Test1()

发现子类Test3获取注解的时候可以获取到父类Test2上@Test1的注解。

现在我们知道@Inherited的作用了吧,详细的功能和分析请查阅其他资料哈,现在咱们言归正传,看下今天的话题。

既然注解@SpringBootApplication也是被@Inherited标注的,那是不是也可以用子类来启动整个项目呢。(估计写sping-boot的大佬也没想到一个启动类竟然还要搞成继承的,哈哈)

测试准备:

spring-boot版本:2.2.0.RELEASE

创建启动类Test

@SpringBootApplication
public class Test {
​
}

创建子类RunTest

public class RunTest extends Test {
 public static void main(String[] args) {
 SpringApplication.run(Test.class, args);
 }
}

运行main函数正常启动。我们修改一下代码如下

public class RunTest extends Test {
 public static void main(String[] args) {
 SpringApplication.run(RunTest.class, args);
 }
}

运行main函数,你会发现什么 what bean没有自动注入!这是什么回事呢?

测试如下代码

Annotation[] annotations = Test.class.getAnnotations();
 Annotation[] annotations1 = RunTest.class.getAnnotations();
 System.out.println(Arrays.stream(annotations).map(annotation -> annotation.toString()).collect(Collectors.joining(",")));
 System.out.println(Arrays.stream(annotations1).map(annotation -> annotation.toString()).collect(Collectors.joining(",")));

获取到的注解为:

@org.springframework.boot.autoconfigure.SpringBootApplication(scanBasePackageClasses={}, proxyBeanMethods=true, exclude={}, excludeName={}, scanBasePackages={})
@org.springframework.boot.autoconfigure.SpringBootApplication(scanBasePackageClasses={}, proxyBeanMethods=true, exclude={}, excludeName={}, scanBasePackages={})

所获取的注解完全一致。那为啥启动类设置Test.class可以正常的运行,而设置成RunTest则自动注入的bean都没有成功注入呢?

如果获取启动类上注解来启动项目的话,理论上讲设置启动类为Test或者RunTest没有申请区别,除非在启动类的加载时,获取注解等信息,并没有获取到父类上的注解。好了,有了这个目标,咱们就可以顺腾摸瓜了。

spring-boot自动装配一文中,我们梳理了整个spring-boot的启动流程,从中我们知道spring-boot启动时,首先会加载几个spring processor和事件监听器还有我们的启动类,然后根据启动类上面的注解一层层的扫描、解析、注入,最终完成整个系统的启动。那么,最根本的问题应该出在主类注入的地方。跟踪代码

org.springframework.boot.SpringApplication#run(java.lang.String...)->
org.springframework.boot.SpringApplication#prepareContext->
org.springframework.boot.SpringApplication#load->
org.springframework.boot.BeanDefinitionLoader#load()->
org.springframework.boot.BeanDefinitionLoader#load(java.lang.Class<?>)->
org.springframework.context.annotation.AnnotatedBeanDefinitionReader#register->
org.springframework.context.annotation.AnnotatedBeanDefinitionReader#doRegisterBean->
org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition#AnnotatedGenericBeanDefinition(java.lang.Class<?>)->
org.springframework.core.type.AnnotationMetadata#introspect->
org.springframework.core.annotation.MergedAnnotations#from(java.lang.reflect.AnnotatedElement, org.springframework.core.annotation.MergedAnnotations.SearchStrategy, org.springframework.core.annotation.RepeatableContainers, org.springframework.core.annotation.AnnotationFilter)->
org.springframework.core.annotation.AnnotationsScanner#isKnownEmpty->
org.springframework.core.annotation.AnnotationsScanner#getDeclaredAnnotations(java.lang.reflect.AnnotatedElement, boolean)

看到getDeclaredAnnotations方法 我们就应该知道为什么了,jdk中class的方法getDeclaredAnnotations就是获取本类上直接注解,是不包含从父类继承过来的注解的。真相大白,可以睡觉去了。

那为啥SpringBootApplication注解被Inherited标注,但是spring启动时却不用到此属性,这是为啥呢?

代码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

错对对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值