第三章 springboot -- 第一节 SpringBoot启动注解 ( @SpringBootApplication )

本文主要是介绍第三章 springboot -- 第一节 SpringBoot启动注解 ( @SpringBootApplication ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SpringBoot 启动注解 @SpringBootApplication 相关知识整理

一、启动类使用

我们可以通过SpringBoot官方网站 https://start.spring.io/创建一个基础的SpringBoot项目
如下示例为 tysite-spark 项目的启动类部分代码

package org.items.tysite;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplication  (1)
public class StartApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(StartApplication.class);}public static void main(String[] args) {SpringApplication.run(StartApplication.class, args);}
}

本章节主要介绍 @SpringBootApplication 注解的都做了那些事情,前面的文章已经介绍过注解的基础知识,本文将忽略基础知识部分的讲解

二、@SpringBootApplication 详解

@SpringBootApplication注解,主要包含@SpringBootConfiguration 、@EnableAutoConfiguration 和@ComponentScan 三个注解

注解源码如下:

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

(1)@SpringBootConfiguration: 该注解继承自 @Configuration,两者功能一致,标注当前类是一个配置类。
注解源码如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
……
}

其下可以通过@Bean标注将方法返回的类对象依赖注入到Spring容器中。

需要注意的是,虽然@Configuration继承自@Component,但@Configuration采用CGLIB的动态代理功能,使 @Bean注释的方法都会被动态代理,调用时通过BeanFactory返回相同的对象。 @Configuration注解是单例模式,@Component注解是多例模式。

(2)@EnableAutoConfiguration :spring的自动装配注解,该注解会根据项目添加的jar包依赖项,完成对项目依赖的自动装配。
注解源码如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";Class<?>[] exclude() default {};String[] excludeName() default {};
}

@EnableAutoConfiguration注解默认会扫描 spring-boot-autoconfigure:xxx.RELEASE.jar包下META-INF/spring.factories文件中,所有org.springframework.boot.autoconfigure.EnableAutoConfiguration值中对应的*Configuration类,并根据类上的条件注解判定结果,将符合条件的配置类加载到spring容器中。·
代码片段如下:

……
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
……

注意:如果项目要剔除某个依赖的自动装配,可以通过本注解的exclude 属性,设置要剔除的配置类即可。(使用上面auto Configure 中的类命名), 示例如下:

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) // -> 禁止数据源自动装配

启动spring时,若开启DEBUG模式,可以在启动日志的CONDITIONS EVALUATION REPORT中查看到项目模块的装配情况
自动装配日志内容截图
注意:
1、@EnableAutoConfiguration通过资源导入注解(@Import(AutoConfigurationImportSelector.class)),将``类引入到启动类中

2、配置类加载规则,通过扫描SpringBoot自动配置包内spring-boot-autoconfigure-*.RELEASE.jar/META-INF/spring.factories文件中org.springframework.boot.autoconfigure.EnableAutoConfiguration属性所配置的所有*AutoConfiguration类,进行装载。在扫描到*AutoConfiguration类时,所有类中的@ConditionalOn*注解生效,判断其所需的类是否存在(或生效),以决定是否装载对应配置。

参考资料:
https://www.cnblogs.com/leihuazhe/p/7743479.html
https://blog.csdn.net/mapleleafforest/article/details/87273213

(3)@ComponentScan : 配置组件自动扫描的指令,以便提供与XML配置<context:component-scan base-package="org.example"/> 相同的功能,继承于@ComponentScans注解。

注解源码如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {@AliasFor("basePackages")String[] value() default {};@AliasFor("value")String[] basePackages() default {};Class<?>[] basePackageClasses() default {};Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;String resourcePattern() default    ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;boolean useDefaultFilters() default true;Filter[] includeFilters() default {};Filter[] excludeFilters() default {};boolean lazyInit() default false;@Retention(RetentionPolicy.RUNTIME)@Target({})@interface Filter {FilterType type() default FilterType.ANNOTATION;@AliasFor("classes")Class<?>[] value() default {};@AliasFor("value")Class<?>[] classes() default {};String[] pattern() default {};}
}

从注解源码中,可以看到@ComponentScan注解的一些重要属性

  • value()basePackages
    属性 互为别名,用于指定@ComponentScan注解扫描的包路径,若不设置则默认从@ComponentScan注释的类开始向下扫描。
  • boolean useDefaultFilters() default true;
    属性 标记是否启用默认过滤规则,该规则用于扫描@Component,@Repository,@Service,@Controller 注解注释的类。
  • includeFilters 属性指定特定扫描规则,通常此时关闭默认扫描规则。
  • excludeFilters 属性用于排除特定扫描规则。
  • @Filter 内部注解可以配合 excludeFiltersincludeFilters 实现 排除 或 增加 某个过滤规则

1、@ComponentScan 常用应用示例:

  1. @ComponentScan(“org.items.tysite”)
    扫描 [org.items.tysite] 包下的所有默认注解,如@Controller、@Service、@Component等
  2. @ComponentScan(basePackageClasses = ThymeleofController.class, useDefaultFilters = false)
    扫描 指定的 ThymeleofController 类
  3. @ComponentScan(value = “org.items.tysite”, includeFilters = { @Filter(type = FilterType.ANNOTATION, value = Component.class) }, useDefaultFilters = false)
    只扫描 [org.items.tysite] 包下,由 @Component注解注释的类
  4. @ComponentScan(value = “org.items.tysite”, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Component.class) })
    扫描 [org.items.tysite] 包下,除@Component注解之外的所有默认规则注解
  5. @ComponentScan(value = “org.items.tysite”, includeFilters = { @Filter(type = FilterType.CUSTOM, value = CustomTypeFilter.class) }, useDefaultFilters = false)
    扫描 [org.items.tysite] 包下,基于CustomTypeFilter 自定义规则加载Bean

2、自定义策略使用案例
以上面示例5的配置作为配置样例,通过实现TypeFilter接口,创建自定义的策略,并通过@Configuration 配置 @ComponentScan

@Configuration
@ComponentScan(value = "org.items.tysite", includeFilters = { @Filter(type = FilterType.CUSTOM, value = CustomTypeFilter.class) }, useDefaultFilters = false)
public class CustomComponentScanConfiguration { }
public class CustomTypeFilter implements TypeFilter {@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {ClassMetadata classMetadata = metadataReader.getClassMetadata();String className = classMetadata.getClassName();if (CommonConsts.class.getName().equals(className)) {System.out.println("成功加载类 ["+CommonConsts.class.getName()+"]");return true;} else {return false;}}
}

注意: 项目使用 @SpringBootApplication 的注解扫描时,以上示例无法生效。因为 @SpringBootApplication注解中排除了 FilterType.CUSTOM 自定义的扫描规则 (详见注解源码)。如果我们的项目中需要使某个类或注解注释不被注入时,可以通过继承TypeExcludeFilter类并重写match方法实现。

match 方法的 MetadataReader 参数,可以获取到扫描对象 类信息、注解信息、以及资源信息,可以根据以上信息判断要注入的类

  //获取扫描到的类注解信息AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();//获取扫描到的类信息ClassMetadata classMetadata = metadataReader.getClassMetadata();//获取扫描到的类资源信息Resource resource = metadataReader.getResource();

参考资料:
https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/core.html#beans-scanning-autodetection

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/TypeExcludeFilter.html#match-org.springframework.core.type.classreading.MetadataReader-org.springframework.core.type.classreading.MetadataReaderFactory-

https://blog.csdn.net/luojinbai/article/details/85877956

这篇关于第三章 springboot -- 第一节 SpringBoot启动注解 ( @SpringBootApplication )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/951280

相关文章

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进