自从用了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. 通俗理解 React 高阶函数

    定义:高阶组件就是一个函数,且该函数接受一个组件作为参数,并返回一个新的组件. A higher-order component is a function that takes a componen ...

  2. ListView加载完数据屏幕会自动和ListView的顶部对齐,而不是布局中最顶部的控件?

    最简单的解决方法 让ListView失去焦点即可 listView.setFocusable(false);

  3. 微信小程序采坑之上拉触底加载更多和下拉刷新

    小程序中加载更多数据一般都是触底刷新 有自带的函数: onReachBottom: function (){} 但是在使用时触发完全没有反应,后来尝试给外层加了一个高度,解决问题 仔细想想也是,没有设 ...

  4. net Core 2.0应用程序发布到IIS

    .net Core 2.0应用程序发布到IIS上注意事项   .net Core2.0应用程序发布window服务器报错容易错过的配置. 1.应用程序发布. 2.IIS上新建网站. 3.应用程序池选择 ...

  5. MapReduce错误之Error: java.lang.RuntimeException: java.lang.NoSuchMethodException的解决方法

    今天跑MapReduce项目的时候遇到了这个问题,日志如下所示: // :: DEBUG ipc.ProtobufRpcEngine: Call: getDiagnostics took 19ms E ...

  6. 《四 spring源码》利用TransactionManager手写spring的aop

    事务控制分类 编程式事务控制          自己手动控制事务,就叫做编程式事务控制. Jdbc代码: Conn.setAutoCommite(false);  // 设置手动控制事务 Hibern ...

  7. 数据库操作是sql的操作1

    项目过来以后, 查 先肯定是做UI , 1.在UI层先加载 2数据来源在dal层, 3再到model层,得到属性 public int MId { get; set; } public string ...

  8. mysql 中unsigned

    整型的每一种都分有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类 ...

  9. ConfigurationErrorsException: Unrecognized configuration section system.data.

    报错 ConfigurationErrorsException: Unrecognized configuration section system.data. (C:\Users\luren\Sou ...

  10. 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here

    你的飞碟在这儿 难度:☆ Code: #include<iostream> #include<cstring> #include<cstdio> using nam ...