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 ...
随机推荐
- Vuex有哪些作用
1.Vuex实现路由数据通讯 点击跳转路由的时候,把写入vuex,跳转后取出vuex
- [TCP/IP]IP协议
IP数据报 IP是TCP/IP协议族中最核心的协议,所有的TCP.UDP.ICMP.IGMP数据都以IP数据报的格式传输.IP仅提供尽力而为的传输服务,如果发生某种错误,IP会丢失该数据,然后发送IC ...
- P2184 贪婪大陆 树状数组
树状数组帅炸了....又被一道水题轻虐,又被学长指出了一个错误....我太菜了QAQ 开两个树状数组,一个记录左端点,一个记录右端点: 共有cnt(总数) - (<l的右端点数目) - (> ...
- CodeForces - 296A-Yaroslav and Permutations(思维)
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...
- 后台任务利器之Hangfire
后台任务利器之Hangfire 一.简述 Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的DI管理着生命周期,免去了在NF4 ...
- 安卓第四次作业——简单校园二手交易APP
一.项目团队 团队成员 姓名:汤文涛 学号:1600802129 班级:计算机164班 博客地址:https://www.cnblogs.com/taotao01/ 姓名:杨圣豪 学号:1600802 ...
- Funsioncharts 线图 破解
在线示例:http://jsfiddle.net/henley/xnozyLa8/2/ 下载:http://files.cnblogs.com/files/ycdx2001/chart.zip
- 重置 linux系统后要配置的基本组件操作
1.安装jdk https://www.cnblogs.com/shihaiming/p/5809553.html 2.安装mysql 3.安装tomcat
- Windows1
① 对Windows的设置一般在, 所有设置, 控制面板(control), 管理方式打开此电脑和此电脑上的选项 ② 关闭对账号安全的检验, 在控制面板中, 找到系统和安全, 再找到更改用户账号控制设 ...
- 一道笔试题和UML思想 ~
一句软件工程界的名言,让我想起了一个和一道笔试题有关的故事.希望更多的人了解 UML 背后的思想比他的语法更重要,是笔者写作本文的一点小愿望. 一.从一句软件工程名言说起 对很多事情的处理上,东西方都 ...