Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章目录
书接上回,《Spring Boot 揭秘与实战 源码分析 - 工作原理剖析》。为了更好的理解 Spring Boot 的 自动配置和工作原理,我们自己来实现一个简单的自动配置模块。
假设,现在项目需要一个功能,需要自动记录项目发布者的相关信息,我们如何通过 Spring Boot 的自动配置,更好的实现功能呢?
实战的开端 – Maven搭建
先创建一个Maven项目,我来手动配置下 POM 文件。
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.3.RELEASE</version>
- </parent>
- <groupId>com.lianggzone.demo</groupId>
- <artifactId>springboot-action-autoconfig</artifactId>
- <version>0.1</version>
- <packaging>jar</packaging>
- <name>springboot-action-autoconfig</name>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
参数的配置 - 属性参数类
首先,我们定义一个自定义前缀,叫做 custom 吧。之前说到,这里的配置参数,可以通过 application.properties 中直接设置。那么,我们创建一个作者的字段,设置默认值为 LiangGzone。
- @ConfigurationProperties(prefix = "custom")
- public class AuthorProperties {
- public static final String DEFAULT_AUTHOR = "LiangGzone";
- public String author = DEFAULT_AUTHOR;
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
那么,聪明的你,应该想到了,我们在 application.properties 中配置的时候,就要这样配置了。
- #custom
- custom.author = 梁桂钊
真的很简单 - 简单的服务类
- public class AuthorServer {
- public String author;
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
你没有看错,真的是太简单了,没有高大上的复杂业务。它的主要用途就是赋值。
自动配置的核心 - 自动配置类
@ConditionalOnClass,参数中对应的类在 classpath 目录下存在时,才会去解析对应的配置类。因此,我们需要配置 AuthorServer 。
@EnableConfigurationProperties, 用来加载配置参数,所以它应该就是属性参数类 AuthorProperties。
- @Configuration
- @ConditionalOnClass({ AuthorServer.class })
- @EnableConfigurationProperties(AuthorProperties.class)
- public class AuthorAutoConfiguration {
- @Resource
- private AuthorProperties authorProperties;
- @Bean
- @ConditionalOnMissingBean(AuthorServer.class)
- @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
- public AuthorServer authorResolver() {
- AuthorServer authorServer = new AuthorServer();
- authorServer.setAuthor(authorProperties.getAuthor());
- return authorServer;
- }
- }
authorResolver方法的作用,即 AuthorProperties 的参数赋值到AuthorServer 中。
spring.factories 不要遗漏
我们需要实现自定义自动装配,就需要自定义 spring.factories 参数。所以,我们需要在 src/main/resources/ META-INF/spring.factories 中配置信息,值得注意的是,这个文件要自己创建。
- # CUSTOM
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.lianggzone.springboot.autoconfig.author.AuthorAutoConfiguration
功能打包与配置依赖
好了,我们已经实现了一个简单的自动配置功能。那么,我们需要将这个项目打成 jar 包部署在我们的本地或者私服上。然后,就可以用了。
我们在另外一个项目中,配置 Maven 依赖。
- <dependency>
- <groupId>com.lianggzone.demo</groupId>
- <artifactId>springboot-action-autoconfig</artifactId>
- <version>0.1</version>
- </dependency>
测试,测试
- @RestController
- @EnableAutoConfiguration
- public class AuthorAutoConfigDemo {
- @Resource
- private AuthorServer authorServer;
- @RequestMapping("/custom/author")
- String home() {
- return "发布者:"+ authorServer.getAuthor();
- }
- }
运行起来,我们看下打印的发布者信息是什么?
我们在 application.properties 中配置一个信息。
- #custom
- custom.author = 梁桂钊
运行起来,我们看下打印的发布者信息是什么?
源代码
相关示例完整代码: springboot-action
(完)
如果觉得我的文章对你有帮助,请随意打赏。

- 版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
- 转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
- 文章标题:Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
- 文章链接:http://blog.720ui.com/2017/springboot_source_autoconfigure_custom/
Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块的更多相关文章
- Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...
- Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机
文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点
文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控
文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控
文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件
文章目录 1. Spring Boot 集成 Java Mail 2. 单元测试 3. 源代码 Spring 对 Java Mail 有很好的支持.因此,Spring Boot 也提供了自动配置的支持 ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎
文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...
- Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ
文章目录 1. 什么是 RabitMQ 2. Spring Boot 整合 RabbitMQ 3. 实战演练4. 源代码 3.1. 一个简单的实战开始 3.1.1. Configuration 3.1 ...
- Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 启用 HTTPS
文章目录 1. 生成证书 2. 配置 HTTPS 支持 3. 启动与测试 4. 源代码 Spring Boot 内嵌的 Tomcat 服务器可以启用 HTTPS 支持. 生成证书 使用第三方 CA 证 ...
随机推荐
- Cassandra V2.1.20单机安装
1. 系统调优 [root@sht-sgmhadoopcm- ~]# echo "vm.max_map_count=131072" >> /etc/sysctl.con ...
- 1.Liunx 文件管理
文件管理命令 1.基本命令:ls .cd .pwd .man 2.文件操作:touch . cp . rm . mv .ln . mkdir . rmdir 3.文件查看:cat .more .les ...
- 【转】js 对象按照键值(不分区大小写)排序,生成签名方法
客户需求小程序端用js生成签名,我们按照要求一步一步解决,并且将请求方法封装到一个utils.js里: 第一步:对关联数组按照键名做正序排序. 第二步:拼接字符串 第三步:将拼接的字符串加上私钥 第四 ...
- 什么是 开发环境、测试环境、生产环境、UAT环境、仿真环境
开发环境:开发环境是程序猿们专门用于开发的服务器,配置可以比较随意, 为了开发调试方便,一般打开全部错误报告. 测试环境:一般是克隆一份生产环境的配置,一个程序在测试环境工作不正常,那么肯定不能把它发 ...
- 线程池 execute 和 submit 的区别
代码示例: public class ThreadPool_Test { public static void main(String[] args) throws InterruptedExcept ...
- jQuery滚屏插件XSwitch.js
1.需要有基本的HTML结构 <div style="margin-top: 124px;" id="container" data-XSwitch> ...
- linux nat style
1● nat style 2● link style
- post和get的使用场景和区别
使用场景: 区别: ①传送方式不同:get通过地址栏传输,post通过报文传输. ②get产生一个TCP数据包,post产生两个数据包,对于get方式的请求,浏览器会把http header和data ...
- BPTT for multiple layers
单层rnn的bptt: 每一个时间点的误差进行反向传播,然后将delta求和,更新本层weight. 多层时: 1.时间1:T 分层计算activation. 2.时间T:1 利用本时间点的误差,分层 ...
- Linux安装Nginx报错: ./configure: error: C compiler cc is not found
CentOS 7.3 下 安装 Nginx 执行配置命令 ./configure 时提示以下错误: checking for OS + Linux 2.6.32-431.el6.x86_64 x86_ ...