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