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文件加上依赖包,问题解决

    <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 @Configuration
    11 @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     @Bean
    16     @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     @Autowired
    11     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对使用的影响。

    今天的故事讲完了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值