栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

spring boot自动配置实现

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot自动配置实现


自从用了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

     1 package com.tanghuachun.teststarter; 2  3 public class TestService { 4  5     private String ip; 6     private int port; 7  8     public TestService(String ip, int port){ 9         this.ip = ip;10         this.port = port;11     }12     13     public void printConfInfo(){14         System.out.println("骚年,你配置的IP为:" + ip + ",端口为:" + port);15     }16 }

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

     1 package com.tanghuachun.teststarter; 2  3 import org.springframework.boot.context.properties.ConfigurationProperties; 4  5 @ConfigurationProperties(prefix = "test-config")//取配置文件前缀为test-config配置 6 public class TestServiceProperties { 7     private String host; 8     private int port; 9 10     public String getHost() {11         return host;12     }13 14     public void setHost(String host) {15         this.host = host;16     }17 18     public int getPort() {19         return port;20     }21 22     public void setPort(int port) {23         this.port = port;24     }25 }

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

     

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

    
        org.springframework.boot
        spring-boot-configuration-processor
        true

    新建一个自动配置类TestServiceAutoConfiguration

     1 package com.tanghuachun.teststarter; 2  3 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 5 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 6 import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 @Configuration11 @ConditionalOnClass(TestService.class)// 存在TestService这个类才装配当前类12 @ConditionalOnProperty(name = "test-config.enabled", havingValue = "true", matchIfMissing = true)//配置文件存在这个test-config.enabled=true才启动,允许不存在该配置13 @EnableConfigurationProperties(TestServiceProperties.class)14 public class TestServiceAutoConfiguration {15     @Bean16     @ConditionalOnMissingBean   // 没有TestService这个类才进行装配17     public TestService testService(TestServiceProperties testServiceProperties) {18         return new TestService(testServiceProperties.getHost(), testServiceProperties.getPort());19     }20 }

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

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

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

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

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

  3. jar包使用

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

    pom文件的依赖配置如下

    
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
                com.tanghuachun
                test-starter
                0.0.1-SNAPSHOT
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            

    在main入口类加上注解

    创建一个TestController类

     1 package com.tanghuachun.testmain; 2  3 import com.tanghuachun.teststarter.TestService; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RestController; 7  8 @RestController 9 public class TestController {10     @Autowired11     private TestService testService;12 13     @GetMapping(value = "/test")14     public String test(){15         testService.printConfInfo();16         return "OK";17     }18 }

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

     

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

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

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

    今天的故事讲完了。

    原文出处:https://www.cnblogs.com/tanghuachun/p/9591538.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号