书接上回,《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. 回收站(recyclebin)及回滚

    1. alter table table_name enable row movement; flashback table table_name  to timestamp  to_timestam ...

  2. CRM WEB UI 02搜索跳转到详细界面

    结合上一个,在上一个中,创建的是选择链接字段EBELN. 下面来实现点击EBELN跳转到详细界面: 1.创建ZLYTEST03_H组件,做详细界面. 2.创建概览页 DETOV. 3.创建视图集 DE ...

  3. free结果解释

    free用于查询内存使用情况,不过其多个数值经常让人感到迷惑,下边对其结果各值进行解释. Mem 1862--总内存/1770--已使用内存/92--未使用内存/199--输出缓存/637--输入缓存 ...

  4. cpu占用过高排查

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 内容解释: PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小 ...

  5. 套接字编程,创建套接字socket

    1.套接字地址结构: struct sockaddr { sa_family_t sa_family; char sa_data[14]; }; 其中,成员sa_family表示套接字的协议族类型,对 ...

  6. 开发Web应用(2)(二十一)

    在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  7. python(6)之文件

    一.读写文件 以追加文件内容(a).读(r).写(w),读写(r+)的形式打开文件: f = open('yesterday','a',encoding='utf-8')#文件句柄 #输出一行文件内容 ...

  8. window7下载安装桌面版ubuntu

    首先需要下载VMware Workstation   下载地址:http://pan.baidu.com/s/1qXS0rhi  秘钥:bbpn 我的环境是ubuntu-14.10-desktop-a ...

  9. Win10系列:UWP界面布局基础6

    资源合并 前面提到过,可以将资源字典定义在单独的XAML文件中,这样的文件被称为资源字典文件.那么,在需要引用文件中的资源时可以通过ResourceDictionary元素的MergedDiction ...

  10. export及export default

    const a=2; const b=3; const c=function(){console.log(a+b} export a; export b; export default c; 如上文件 ...