书接上回,《Spring Boot 揭秘与实战 源码分析 - 工作原理剖析》。为了更好的理解 Spring Boot 的 自动配置和工作原理,我们自己来实现一个简单的自动配置模块。

假设,现在项目需要一个功能,需要自动记录项目发布者的相关信息,我们如何通过 Spring Boot 的自动配置,更好的实现功能呢?

实战的开端 – Maven搭建

先创建一个Maven项目,我来手动配置下 POM 文件。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>1.3.3.RELEASE</version>
  8. </parent>
  9. <groupId>com.lianggzone.demo</groupId>
  10. <artifactId>springboot-action-autoconfig</artifactId>
  11. <version>0.1</version>
  12. <packaging>jar</packaging>
  13. <name>springboot-action-autoconfig</name>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-autoconfigure</artifactId>
  18. </dependency>
  19. </dependencies>
  20. <build>
  21. <plugins>
  22. <plugin>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-maven-plugin</artifactId>
  25. </plugin>
  26. </plugins>
  27. </build>
  28. </project>

参数的配置 - 属性参数类

首先,我们定义一个自定义前缀,叫做 custom 吧。之前说到,这里的配置参数,可以通过 application.properties 中直接设置。那么,我们创建一个作者的字段,设置默认值为 LiangGzone。

  1. @ConfigurationProperties(prefix = "custom")
  2. public class AuthorProperties {
  3. public static final String DEFAULT_AUTHOR = "LiangGzone";
  4. public String author = DEFAULT_AUTHOR;
  5. public String getAuthor() {
  6. return author;
  7. }
  8. public void setAuthor(String author) {
  9. this.author = author;
  10. }
  11. }

那么,聪明的你,应该想到了,我们在 application.properties 中配置的时候,就要这样配置了。

  1. #custom
  2. custom.author = 梁桂钊

真的很简单 - 简单的服务类

  1. public class AuthorServer {
  2. public String author;
  3. public String getAuthor() {
  4. return author;
  5. }
  6. public void setAuthor(String author) {
  7. this.author = author;
  8. }
  9. }

你没有看错,真的是太简单了,没有高大上的复杂业务。它的主要用途就是赋值。

自动配置的核心 - 自动配置类

@ConditionalOnClass,参数中对应的类在 classpath 目录下存在时,才会去解析对应的配置类。因此,我们需要配置 AuthorServer 。

@EnableConfigurationProperties, 用来加载配置参数,所以它应该就是属性参数类 AuthorProperties。

  1. @Configuration
  2. @ConditionalOnClass({ AuthorServer.class })
  3. @EnableConfigurationProperties(AuthorProperties.class)
  4. public class AuthorAutoConfiguration {
  5. @Resource
  6. private AuthorProperties authorProperties;
  7. @Bean
  8. @ConditionalOnMissingBean(AuthorServer.class)
  9. @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
  10. public AuthorServer authorResolver() {
  11. AuthorServer authorServer = new AuthorServer();
  12. authorServer.setAuthor(authorProperties.getAuthor());
  13. return authorServer;
  14. }
  15. }

authorResolver方法的作用,即 AuthorProperties 的参数赋值到AuthorServer 中。

spring.factories 不要遗漏

我们需要实现自定义自动装配,就需要自定义 spring.factories 参数。所以,我们需要在 src/main/resources/ META-INF/spring.factories 中配置信息,值得注意的是,这个文件要自己创建。

  1. # CUSTOM
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.lianggzone.springboot.autoconfig.author.AuthorAutoConfiguration

功能打包与配置依赖

好了,我们已经实现了一个简单的自动配置功能。那么,我们需要将这个项目打成 jar 包部署在我们的本地或者私服上。然后,就可以用了。

我们在另外一个项目中,配置 Maven 依赖。

  1. <dependency>
  2. <groupId>com.lianggzone.demo</groupId>
  3. <artifactId>springboot-action-autoconfig</artifactId>
  4. <version>0.1</version>
  5. </dependency>

测试,测试

  1. @RestController
  2. @EnableAutoConfiguration
  3. public class AuthorAutoConfigDemo {
  4. @Resource
  5. private AuthorServer authorServer;
  6. @RequestMapping("/custom/author")
  7. String home() {
  8. return "发布者:"+ authorServer.getAuthor();
  9. }
  10. }

运行起来,我们看下打印的发布者信息是什么?

我们在 application.properties 中配置一个信息。

  1. #custom
  2. custom.author = 梁桂钊

运行起来,我们看下打印的发布者信息是什么?

源代码

相关示例完整代码: springboot-action

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块的更多相关文章

  1. Spring Boot 揭秘与实战 源码分析 - 工作原理剖析

    文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...

  2. Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机

    文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...

  3. Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点

    文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...

  4. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控

    文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...

  5. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控

    文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...

  6. Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件

    文章目录 1. Spring Boot 集成 Java Mail 2. 单元测试 3. 源代码 Spring 对 Java Mail 有很好的支持.因此,Spring Boot 也提供了自动配置的支持 ...

  7. Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎

    文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...

  8. Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ

    文章目录 1. 什么是 RabitMQ 2. Spring Boot 整合 RabbitMQ 3. 实战演练4. 源代码 3.1. 一个简单的实战开始 3.1.1. Configuration 3.1 ...

  9. Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 启用 HTTPS

    文章目录 1. 生成证书 2. 配置 HTTPS 支持 3. 启动与测试 4. 源代码 Spring Boot 内嵌的 Tomcat 服务器可以启用 HTTPS 支持. 生成证书 使用第三方 CA 证 ...

随机推荐

  1. MSMQ 事务性消息处理

    二.事务性消息处理 事务我想大家对这个词应该都不会陌生,在操作数据库的时候经常都会用到事务,确保操作成功,要么全部完成(成功) ,要么全部不完成(失败).在MSMQ中利用事务性处理,可以确保事务中的消 ...

  2. 移除input框type="number"在部分浏览器的默认上下按钮

    input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none !impor ...

  3. PHP手册-函数参考-日期与时间相关扩展

    一.Calander.日期/时间.HRTime扩展的对比   Calendar 日期/时间 HRTime 简介 历法扩展集包括了一系列用于在不同历法间进行转换的函数,它是以Julian Day计数为中 ...

  4. zzw原创_根据某一文件复制出大量固定位数后缀名的递增的文件

    1.trre.sh   :根据某一文件复制出大量固定位数后后缀递增的文件.   如将 SPINFO_190516_20170109.001 复制成SPINFO_190516_20170109.002  ...

  5. How to import a GitHub project into Eclipse

    Assuming you have created a project in GitHub, these are the steps to import it into Eclipse. First, ...

  6. WinForm之窗体应用程序

    WinForm之窗体应用程序 基本简单数据库操作(增删改查) using System; using System.Collections.Generic; using System.Windows. ...

  7. js之添加浏览器历史记录

    如何生成一条历史记录 简单粗暴的方法,直接在当前页面的地址栏中输入地址 点击页面中有a标签的href 执行location.href = ‘xxx’(location.replace(‘xxx’)生成 ...

  8. codeigniter在nginx下返回404 not found

    codeigniter框架需要path_info的支持,Apache默认支持path_info,但是nginx默认不支持,我们需要设置nginx,使得nginx支持path_info 网上试了好多方法 ...

  9. day29-python阶段性复习三

    七.python打开文件方式 open r: 读的方式 w:已写的方式打开 a:以追加的方式 r+ 读写模式 w+ 读写 a+ 读写 rb:二进制读模式打开 wb:以二进制写模式打开 ab 二进制追加 ...

  10. mysql插入中文乱码

    https://www.cnblogs.com/zhchoutai/p/7364835.html 最简单的一招,不用修改my.ini文件: 1.停掉mysql服务 2.启动:X:\%path%\MyS ...