zoukankan      html  css  js  c++  java
  • Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块

    文章目录

    1. 1. 实战的开端 – Maven搭建
    2. 2. 参数的配置 - 属性参数类
    3. 3. 真的很简单 - 简单的服务类
    4. 4. 自动配置的核心 - 自动配置类
    5. 5. spring.factories 不要遗漏
    6. 6. 功能打包与配置依赖
    7. 7. 测试,测试
    8. 8. 源代码

    书接上回,《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.  
    5. @Resource
    6. private AuthorServer authorServer;
    7.  
    8. @RequestMapping("/custom/author")
    9. String home() {
    10. return "发布者:"+ authorServer.getAuthor();
    11. }
    12. }

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

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

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

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

    源代码

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

    (完)

    微信公众号
  • 相关阅读:
    网易163邮箱被盗号找回经历
    C++中基类的析构函数为什么要用virtual虚析构函数
    像linux ls命令一样优雅地打印
    【Linux】- 六个超赞的字符画生成器
    linux欢迎界面 /etc/motd
    Linux 的 FIGlet 指令产生 ASCII Art 大型文字教学
    趣玩 Linux:四个生成字符图案(字符画)的命令
    案例参考手册-第四章 Curses字符界面.docx
    读取键盘输入流改为原始模式
    centos 7配置系统调度isolcpus(软中断绑定)
  • 原文地址:https://www.cnblogs.com/cnblog-long/p/7251440.html
Copyright © 2011-2022 走看看