09.05 牛人讲解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
  2. maven版本号:3.5.4

jar包封装

  1. 创建一个springboot项目
牛人讲解spring boot自动配置实现


  1. 填写坐标信息
牛人讲解spring boot自动配置实现


  1. springboot版本2.0.4
牛人讲解spring boot自动配置实现


  1. 其他默认,创建完成后,目录如下
牛人讲解spring boot自动配置实现


  1. 接下来创建我们的测试服务类TestService
牛人讲解spring boot自动配置实现


 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 }
牛人讲解spring boot自动配置实现


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


 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 }
牛人讲解spring boot自动配置实现


  1. 我们发现在这个类写好后提示有错误
  2. 在pom文件加上依赖包,问题解决
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-configuration-processor/<artifactid>
<optional>true/<optional>
/<dependency>
  1. 新建一个自动配置类TestServiceAutoConfiguration
牛人讲解spring boot自动配置实现


 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 }
牛人讲解spring boot自动配置实现


  1. 相关注解含义在注释中已经说明,到这里,代码已经写好了,我们希望以注解的方式给用户使用,自定义一个注解@EnableTestService
牛人讲解spring boot自动配置实现


 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 }
牛人讲解spring boot自动配置实现


  1. 然后注释掉pom文件中的maven插件,如下图
牛人讲解spring boot自动配置实现


  1. 然后maven打包,就会生成一个jar包,这个jar包我们就可以直接用了
牛人讲解spring boot自动配置实现


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

jar包使用

  1. 我们开始来测试我们封装好的jar,首先创建一个springboot项目(创建时一路默认,你的包名也可以和我一样,无所谓的)
牛人讲解spring boot自动配置实现


  1. pom文件的依赖配置如下
牛人讲解spring boot自动配置实现


<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>
牛人讲解spring boot自动配置实现


  1. 在main入口类加上注解
牛人讲解spring boot自动配置实现


  1. 创建一个TestController类
牛人讲解spring boot自动配置实现


 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 }
牛人讲解spring boot自动配置实现


  1. 接下来在application.properties中配置参数
  2. 启动该项目,在地址栏输入 http://localhost:8080/test 回车,看控制台打印的信息恰好是我们需要的
牛人讲解spring boot自动配置实现


  1. 到这里我们就完成了一个自动配置的封装。
  2. 骚年们可以研究一下starter中注解@ConditionalOnProperty对使用的影响。
  3. 今天的故事讲完了。
牛人讲解spring boot自动配置实现

牛人讲解spring boot自动配置实现


分享到:


相關文章: