springboot对第三方包的整合是由maven依赖和starter插件完成的。
比如spring-boot-starter-web、spring-boot-starter-data-redis、spring-boot-starter-jdbc等等。
如果要自定义starter,我们以redis的整合包为例,Ctrl+左键点击进入redis依赖,可以看到里面有一个spring-boot-test-autoconfigure依赖,点进这个依赖,里面还有一个spring-boot-autoconfigure依赖,我们copy这个自动配置的依赖到自己的自定义插件的项目中。
自定义插件,新建一个maven项目名为token-redis-springboot-starter,依赖引入上面那个自动配置的依赖以及
spring-boot-starter-parent依赖。
创建三个类,结构如下
首先TokenProperties类
package com.vhukze.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "vhukze")
public class TokenProperties {
private String tokenRedisHost;
private String tokenRedisPwd;
public void setTokenRedisHost(String tokenRedisHost) {
this.tokenRedisHost = tokenRedisHost;
}
public void setTokenRedisPwd(String tokenRedisPwd) {
this.tokenRedisPwd = tokenRedisPwd;
}
public String getTokenRedisHost() {
return tokenRedisHost;
}
public String getTokenRedisPwd() {
return tokenRedisPwd;
}
}
TokenService类
package com.vhukze.service;
import com.vhukze.utils.TokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class TokenService {
@Autowired
private TokenProperties tokenProperties;
public String getToken(){
return tokenProperties.getTokenRedisHost()+"---"+tokenProperties.getTokenRedisPwd();
}
}
TokenAutoConfiguration类
package com.vhukze.config;
import com.vhukze.service.TokenService;
import com.vhukze.utils.TokenProperties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(TokenProperties.class)
public class TokenAutoConfiguration {
@Bean
public TokenService tokenService(){
return new TokenService();
}
}
在resources文件夹下创建一个spring.factories配置文件,用来注册自动配置类,内容如下,如果有多个配置类 用逗号隔开
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.vhukze.config.TokenAutoConfiguration
到这里自定义的starter就完成了,进入项目路径,执行mvn clean install命令把项目打包进本地maven仓库
创建一个测试项目 test-springboot 把自定的starter依赖引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.vhukze</groupId>
<artifactId>token-redis-springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
在resources下创建application.yml配置文件,配置文件中的key没有大写,他会自动把你的驼峰式命名转换成-形式
vhukze:
token-redis-host: 127.0.0.1
token-redis-pwd: 12345
创建一个controller用来测试
package com.vhukze.controller;
import com.vhukze.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class IndexController {
@Autowired
private TokenService tokenService;
public static void main(String[] args) {
SpringApplication.run(IndexController.class);
}
@RequestMapping("/test")
public String test(){
return tokenService.getToken();
}
}
访问localhost:8080/test