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 证 ...
随机推荐
- 回收站(recyclebin)及回滚
1. alter table table_name enable row movement; flashback table table_name to timestamp to_timestam ...
- CRM WEB UI 02搜索跳转到详细界面
结合上一个,在上一个中,创建的是选择链接字段EBELN. 下面来实现点击EBELN跳转到详细界面: 1.创建ZLYTEST03_H组件,做详细界面. 2.创建概览页 DETOV. 3.创建视图集 DE ...
- free结果解释
free用于查询内存使用情况,不过其多个数值经常让人感到迷惑,下边对其结果各值进行解释. Mem 1862--总内存/1770--已使用内存/92--未使用内存/199--输出缓存/637--输入缓存 ...
- cpu占用过高排查
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 内容解释: PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小 ...
- 套接字编程,创建套接字socket
1.套接字地址结构: struct sockaddr { sa_family_t sa_family; char sa_data[14]; }; 其中,成员sa_family表示套接字的协议族类型,对 ...
- 开发Web应用(2)(二十一)
在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- python(6)之文件
一.读写文件 以追加文件内容(a).读(r).写(w),读写(r+)的形式打开文件: f = open('yesterday','a',encoding='utf-8')#文件句柄 #输出一行文件内容 ...
- window7下载安装桌面版ubuntu
首先需要下载VMware Workstation 下载地址:http://pan.baidu.com/s/1qXS0rhi 秘钥:bbpn 我的环境是ubuntu-14.10-desktop-a ...
- Win10系列:UWP界面布局基础6
资源合并 前面提到过,可以将资源字典定义在单独的XAML文件中,这样的文件被称为资源字典文件.那么,在需要引用文件中的资源时可以通过ResourceDictionary元素的MergedDiction ...
- export及export default
const a=2; const b=3; const c=function(){console.log(a+b} export a; export b; export default c; 如上文件 ...