springboot面试之核心注解有哪些

springboot面试之核心注解有哪些,第1张

springboot面试之核心注解有哪些

@SpringBootApplication注解是最核心的注解,使用在main方法所在的类上,代表着是一个springboot的应用程序,该注解源码如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}

其中比较重要的有@SpringBootConfiguration,该注解组合了@Configuration,代表着是一个Java config,@Configuration注解是spring-context提供的注解,不是springboot提供的,源码如下:

// org.springframework.context.annotation.Configuration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@Component
public @interface Configuration {

	@AliasFor(annotation = Component.class)
	String value() default "";

}

注解EnableAutoConfiguration是springboot中spring-boot-autoconfigure模块提供的注解,意思是启用自动配置,我们使用springboot,可以说就是使用其自动配置的功能,所以说该注解也是非常重要的,源码如下:

// org.springframework.boot.autoconfigure.EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@AutoConfigurationPackage
@import(AutoConfigurationimportSelector.class)
public @interface EnableAutoConfiguration {

	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

	// 不进行自动配置的类的class对象数组
	Class[] exclude() default {};

    // 不进行配置的类的spring bean名称数组
	String[] excludeName() default {};
}

注解@ComponentScan,用来配置要扫描的包路径,这是来自spring-context模块的注解,源码如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
	@AliasFor("basePackages")
	String[] value() default {};

	@AliasFor("value")
	String[] basePackages() default {};
    
    // ...snip...
}

项目关于springboot中使用到的注解可以参考这篇文章。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4968965.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-13

发表评论

登录后才能评论

评论列表(0条)

    保存