自从用了spring boot,都忘记spring mvc中的xml配置是个什么东西了,再也回不去。为啥spring boot这么好用呢, 约定大于配置的设计初衷, 让我们只知道维护好application.properties(或application.yml)文件就可以了,我们在配置文件里可以设置数据源参数,可以设置服务端口,可以设置redis的地址等等。我们经常会看一些包含starter名字的jar包,如spring-boot-starter-data-redis,引入这些jar包,我们就可以简单快速配置了。那么我们自己开发了一个接口服务给别人调用,我们是不是可以把它封装成一个starter jar包呢?让别人在application.properties定义,实现自动配置呢?答案是允许的,下面跟我一起写一个自动配置jar包。(本文的目的不是讲解自动配置的原理,大家可以自行网上搜索原理)。

  1. 环境信息

    开发工具:idea

    maven版本号:3.5.4

  2. jar包封装

    创建一个springboot项目

    填写坐标信息

    springboot版本2.0.4

    其他默认,创建完成后,目录如下

    接下来创建我们的测试服务类TestService

     package com.tanghuachun.teststarter;
    
     public class TestService {
    
         private String ip;
    private int port; public TestService(String ip, int port){
    this.ip = ip;
    this.port = port;
    } public void printConfInfo(){
    System.out.println("骚年,你配置的IP为:" + ip + ",端口为:" + port);
    }
    }

    我们设想,别人使用我们这个接口的时候,将Ip和Port通过application.properties注入,接下来我们创建属性配置实体类TestServiceProperties

     package com.tanghuachun.teststarter;
    
     import org.springframework.boot.context.properties.ConfigurationProperties;
    
     @ConfigurationProperties(prefix = "test-config")//取配置文件前缀为test-config配置
    public class TestServiceProperties {
    private String host;
    private int port; public String getHost() {
    return host;
    } public void setHost(String host) {
    this.host = host;
    } public int getPort() {
    return port;
    } public void setPort(int port) {
    this.port = port;
    }
    }

    我们发现在这个类写好后提示有错误

    在pom文件加上依赖包,问题解决

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
    </dependency>

    新建一个自动配置类TestServiceAutoConfiguration

     package com.tanghuachun.teststarter;
    
     import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration; @Configuration
    @ConditionalOnClass(TestService.class)// 存在TestService这个类才装配当前类
    @ConditionalOnProperty(name = "test-config.enabled", havingValue = "true", matchIfMissing = true)//配置文件存在这个test-config.enabled=true才启动,允许不存在该配置
    @EnableConfigurationProperties(TestServiceProperties.class)
    public class TestServiceAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean // 没有TestService这个类才进行装配
    public TestService testService(TestServiceProperties testServiceProperties) {
    return new TestService(testServiceProperties.getHost(), testServiceProperties.getPort());
    }
    }

    相关注解含义在注释中已经说明,到这里,代码已经写好了,我们希望以注解的方式给用户使用,自定义一个注解@EnableTestService

     package com.tanghuachun.teststarter;
    import org.springframework.context.annotation.Import;
    import java.lang.annotation.*; @Inherited
    @Documented
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Import(TestServiceAutoConfiguration.class)
    //相当于使用定义spring.factories完成Bean的自动装配
    public @interface EnableTestService {
    //@Import(TestServiceAutoConfiguration.class) 需要在调用者的Main 类加上该注解就能等效于spring.factories 文件配置
    }

    然后注释掉pom文件中的maven插件,如下图

    然后maven打包,就会生成一个jar包,这个jar包我们就可以直接用了

    这里因为在本地环境测试,我们将编译好的jar安装到本地maven仓库,点击右边的install按钮(你也可以导入编译好的jar包一样的)

  3. jar包使用

    我们开始来测试我们封装好的jar,首先创建一个springboot项目(创建时一路默认,你的包名也可以和我一样,无所谓的)

    pom文件的依赖配置如下

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency> <dependency>
    <groupId>com.tanghuachun</groupId>
    <artifactId>test-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency> <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

    在main入口类加上注解

    创建一个TestController类

     package com.tanghuachun.testmain;
    
     import com.tanghuachun.teststarter.TestService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController; @RestController
    public class TestController {
    @Autowired
    private TestService testService; @GetMapping(value = "/test")
    public String test(){
    testService.printConfInfo();
    return "OK";
    }
    }

    接下来在application.properties中配置参数

    启动该项目,在地址栏输入 http://localhost:8080/test 回车,看控制台打印的信息恰好是我们需要的

    到这里我们就完成了一个自动配置的封装。

    骚年们可以研究一下starter中注解@ConditionalOnProperty对使用的影响。

    今天的故事讲完了。

spring boot自动配置实现的更多相关文章

  1. Springboot 系列(三)Spring Boot 自动配置原理

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...

  2. Spring Boot自动配置与Spring 条件化配置

    SpringBoot自动配置 SpringBoot的自动配置是一个运行时(应用程序启动时)的过程,简化开发时间,无需浪费时间讨论具体的Spring配置,只需考虑如何利用SpringBoot的自动配置即 ...

  3. Spring Boot自动配置原理、实战

    Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...

  4. Spring Boot自动配置

    Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...

  5. Spring boot 自动配置自定义配置文件

    示例如下: 1.   新建 Maven 项目 properties 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

  6. Spring Boot自动配置原理与实践(一)

    前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...

  7. Spring Boot自动配置原理(转)

    第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot- ...

  8. Spring Boot自动配置如何工作

    通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...

  9. Spring boot --- 自动配置

    spring boot 自动配置 指的是针对很多spring 应用程序常见的应用功能,spring boot 能自动提供相关配置. spring boot 自动配置加载     Spring boot ...

  10. 使用@AutoConfigureBefore、After、Order调整Spring Boot自动配置顺序

    前言 Spring Boot是Spring家族具有划时代意义的一款产品,它发展自Spring Framework却又高于它,这种高于主要表现在其最重要的三大特性,而相较于这三大特性中更为重要的便是Sp ...

随机推荐

  1. ADO学途 one day

    博主在第一个demo的困苦中,略得一些见解,需要的伙伴可以get下 刚开始先创建一个程序的前端,这样对自己要实现的功能有一个直观的了解.分三 步:首先1.创建一个windows窗体.拖控件使程序接近真 ...

  2. easyui datagrid编辑时编辑框自动获取焦点

    onDblClickCell:function(rowIndex, field, val){//双击单元格监听器 $(this).datagrid('beginEdit',rowIndex);//开启 ...

  3. NET Core 2.0 介绍和使用

    NET Core 2.0 特性介绍和使用指南 阅读目录 前言 特性概述 使用指南 .NET Core 2.0和1.0/1.1之间的关系 .NET CORE Rumtime改进 .NET Core SD ...

  4. [USACO07JAN]平衡的阵容Balanced Lineup

    [USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...

  5. IO流----File,递归,字节流,字符流

    要把数据持久化存储,就需要把内存中的数据存储到内存以外的其他持久化设备(硬盘.光盘.U盘等)上. 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读 ...

  6. 去除inline-block间隙的几种方法

    为什么会产生间隙? 由于编写代码时的美观和可读性,在代码中添加回车或空格而产生的间隙. html代码: <ul class="container"> <li> ...

  7. Azure 门户使用概览

    Azure 门户是管理 Azure 云平台的核心工具,用户可以在其中预配和管理 Azure 资源.本教程将帮助你熟悉Azure管理门户,包括一些关键功能的介绍,并演示了如何通过 Azure 门户创建虚 ...

  8. CentOS7.2上搭建httpbin环境

    CentOS7上搭建httpbin环境 1.安装python31)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-de ...

  9. iOS Automated Tests with UIAutomation

    参照:http://blog.manbolo.com/2012/04/08/ios-automated-tests-with-uiautomation#1 UI Automation JavaScri ...

  10. Vue风格指南总结及对应ESLint规则配置

    全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/10906951.html,多谢,=.=~ 必要的:规避错误: 强烈推荐:改善可读性和开发体验: 推 ...