亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

spring boot自動配置實現

標簽:
Java SpringBoot


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

    https://img1.sycdn.imooc.com//5b8f667d000125c208860597.jpg

    填写坐标信息

    https://img1.sycdn.imooc.com//5b8f668600015f5e08860597.jpg

    springboot版本2.0.4

    https://img1.sycdn.imooc.com//5b8f668c0001871608860597.jpg

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

    https://img1.sycdn.imooc.com//5b8f66930001e5f404810522.jpg

    接下来创建我们的测试服务类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 }

    复制代码

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

     https://img1.sycdn.imooc.com//5b8f66be000173ba15620547.jpg

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

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

    新建一个自动配置类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文件的依赖配置如下

    复制代码

    <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类

    复制代码

     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

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消