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包。(本文的目的不是讲解自动配置的原理,大家可以自行网上搜索原理)。
环境信息
开发工具:idea
maven版本号:3.5.4
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包一样的)
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自动配置实现的更多相关文章
- Springboot 系列(三)Spring Boot 自动配置原理
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...
- Spring Boot自动配置与Spring 条件化配置
SpringBoot自动配置 SpringBoot的自动配置是一个运行时(应用程序启动时)的过程,简化开发时间,无需浪费时间讨论具体的Spring配置,只需考虑如何利用SpringBoot的自动配置即 ...
- Spring Boot自动配置原理、实战
Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...
- Spring Boot自动配置
Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...
- Spring boot 自动配置自定义配置文件
示例如下: 1. 新建 Maven 项目 properties 2. pom.xml <project xmlns="http://maven.apache.org/POM/4 ...
- Spring Boot自动配置原理与实践(一)
前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...
- Spring Boot自动配置原理(转)
第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot- ...
- Spring Boot自动配置如何工作
通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...
- Spring boot --- 自动配置
spring boot 自动配置 指的是针对很多spring 应用程序常见的应用功能,spring boot 能自动提供相关配置. spring boot 自动配置加载 Spring boot ...
- 使用@AutoConfigureBefore、After、Order调整Spring Boot自动配置顺序
前言 Spring Boot是Spring家族具有划时代意义的一款产品,它发展自Spring Framework却又高于它,这种高于主要表现在其最重要的三大特性,而相较于这三大特性中更为重要的便是Sp ...
随机推荐
- 日期和时间-time时间模块
时间的检测 #时间的检测 #导入时间模块 import time #返回当前时区与格林尼治所在时区的相差秒数(推荐) print(time.timezone) #输出结果:-28800 #返回当前时区 ...
- Nginx 最新版源码编译安装 包含常用模块作用及所需依赖
第一部分 Nginx最新版源码编译安装 1. 使用的模块 模块1:http_rewrite_module 基于正则匹配来实现重定向.依赖PCRE库,见依赖1 模块2:http_gzip_module ...
- java基础第五篇封装与面向对象
a.方法: public static void main(String[] args) { } 一般定义标准: 形参:一般把 不确定的量或者变化的量定义在形参位置//圆的的半径,长方形的长和宽,传递 ...
- A JAX-WS web service is by itself a Singleton
http://stackoverflow.com/questions/11096310/singleton-object-in-java-web-service http://stackoverflo ...
- spring boot 参数转换
参数调用方式: 1. localhost:8080/person/properties/to/json body参数设置: 2. localhost:8080/person/json/to/prope ...
- NET Core写了一个轻量级的Interception框架[开源]
NET Core写了一个轻量级的Interception框架[开源] ASP.NET Core具有一个以ServiceCollection和ServiceProvider为核心的依赖注入框架,虽然这只 ...
- LWIP应用指南学习。
一 TCP接口函数:tcp_init() 必须在调用其它TCP函数之前调用,必须用一个硬件定时器来配置每TCP_FAST_INTERVAL (ms)调用一次tcp_fasttmr() :每TCP_SL ...
- 把本地项目上传到github 不使用eclipse
https://blog.csdn.net/m0_37725003/article/details/80904824
- 【部分补充】【翻译转载】【官方教程】Asp.Net MVC4入门指南(4):添加一个模型
4. 添加一个模型 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-mo ...
- java进程占用系统内存高,排查方法
查看所有内存占用情况 top 定位线程问题(通过命令查看16764 进程的线程情况) ps p -L -o pcpu,pmem,pid,tid,time,tname,cmd 计数 ps p -L -o ...