内容简介:也许你有一个内部库包或一个在整个应用程序环境中常用的开源库,如果要在多个Spring Boot应用程序中使用它,为它创建一个Spring Boot启动器可能会很有用。Spring Boot启动器包含两个模块:让我们看一下名为@autoconfiguration的机制。这听起来比实际更复杂。
也许你有一个内部库包或一个在整个应用程序环境中常用的开源库,如果要在多个Spring Boot应用程序中使用它,为它创建一个Spring Boot启动器可能会很有用。
总观
Spring Boot启动器包含两个模块:
- 自动配置 ,这是执行繁重工作和设置模块
- 启动 启动程序模块,它将您的lib库包、@autoconfiguration和所有依赖项包装在一个依赖项中
让我们看一下名为@autoconfiguration的机制。这听起来比实际更复杂。
在启动时,Spring Boot会扫描类路径, 查找 位于 META-INF 目录中名为 spring.factories的 所有文件,并对其进行处理。这些文件包含单个键 org.springframework.boot.autoconfigure.EnableAutoConfiguration = ,其值设置为常规 @Configurtion 类的列表。
它会检查每个 @Configurtion 是否应该包含它并最终使用它。所谓使用就是加入Spring上下文,这样能被别人发现,通过@autowired自动注入这些类。
当Spring Boot考虑使用它时,将添加条件; 就像 @ConditionalOnClass 一样,它添加了一个条件,只有当类路径中存在指定的 @Configurtion 类时才会将这个类包含进来。
实现自动配置
Spring Boot建议为模块命名模式: -spring-boot-autoconfigure , 还要声明我们应该为所有属性添加前缀,以免弄乱任何名称空间。让我们尊重它。
比如这里我们的模块将命名为 request-logging-spring-boot-autoconfigure, 并包含两个类 - @ Confiration类和一个属性类,用于公开可在 application.properties中 配置的一些属性。
让我们从pom开始吧。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
</dependency>
前两个依赖项是使用Spring Boot和自动配置类, spring-boot-configuration-processor 是一个从 @ConfigurationProperties 创建元数据文件的工具
下面是配置属性类
@ConfigurationProperties(prefix = "com.requestlogging")
public class RequestLoggingProperties {
.....
}
下面一步定义 RequestLoggingAutoConfiguration :我们使用 @EnableConfigurationProperties 启用属性 并添加两个条件:
- @ConditionalOnWebApplication 表示只有Spring Boot应用程序是Web应用程序时才包含我们的配置。
- @ConditionalOnClass 定义 RequestContextLoggingFilter 必须存在于类路径才能包含配置它。
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(RequestContextLoggingFilter.class)
@EnableConfigurationProperties(RequestLoggingProperties.class)
public class RequestLoggingAutoConfiguration {
@Autowired
private RequestLoggingProperties requestLoggingProperties;
@Bean
@Order(1)
@ConditionalOnMissingBean
public RequestContextLoggingFilter requestContextLoggingFilter() {
return new RequestContextLoggingFilter(requestLoggingProperties.getRequestHeaderId(),
requestLoggingProperties.getLogIdentifier());
}
}
如果Spring Boot包含了我们这个配置,那么如果 @ConditionalOnMissingBean 有效,它将初始化我们的过滤器bean:new一个RequestContextLoggingFilter实例 。只有当我们的Spring上下文中没有bean存在,我们的bean才会被初始化。
使用autoconfigure模块, Spring建议为启动器命名模式: -spring-boot-starter . 这里我们命名为 request-logging-spring-boot-starter :
<dependency> <groupId>com.jdon</groupId> <artifactId>request-logging-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
启动器需要包含使此启动器工作的所有活动依赖项,否则无法工作。
这样别人就可以使用上面配置使用你的库包了。
Spring Boot
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Spring Boot的gRPC启动器
- Windows Phone开发手记-WinRT下启动器替代方案
- Windows Phone开发手记-WinRT下启动器替代方案
- 微软安卓启动器将加入时间线功能,串联 Windows 服务
- 苞米豆 — 谷歌验证码快速启动器 1.1.0 发布
- Windows Phone开发手记-WinRT下启动器替代方案
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Compilers
Alfred V. Aho、Monica S. Lam、Ravi Sethi、Jeffrey D. Ullman / Addison Wesley / 2006-9-10 / USD 186.80
This book provides the foundation for understanding the theory and pracitce of compilers. Revised and updated, it reflects the current state of compilation. Every chapter has been completely revised ......一起来看看 《Compilers》 这本书的介绍吧!