xxxx-spring-boot-starter
1、pom.xml
<groupId>com.maykit</groupId>
<artifactId>token-redis-springboot-starter</artifactId>
<version>0.0.3</version>
<name>token-redis-springboot-starter</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.3.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--application.properties 文件自动提示 必加如下jar
mayikt.token-redis-host=127.0.0.1
mayikt.token-redis-pwd=root
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
2、spring.factories
resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mayikt.config.TokenAutoConfiguration
3、Properties
package com.mayikt.utils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
/*@ConditionalOnProperty(prefix = "mayikt")*/
@ConfigurationProperties("mayikt")
public class TokenProperties {
private String tokenRedisHost;
private String tokenRedisPwd;
public String getTokenRedisHost() {
return tokenRedisHost;
}
public void setTokenRedisHost(String tokenRedisHost) {
this.tokenRedisHost = tokenRedisHost;
}
public String getTokenRedisPwd() {
return tokenRedisPwd;
}
public void setTokenRedisPwd(String tokenRedisPwd) {
this.tokenRedisPwd = tokenRedisPwd;
}
@Override
public String toString() {
return "TokenProperties{" +
"tokenRedisHost='" + tokenRedisHost + '\'' +
", tokenRedisPwd='" + tokenRedisPwd + '\'' +
'}';
}
}
4、config
package com.mayikt.config;
import com.mayikt.utils.TokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class TokenService {
@Autowired
private TokenProperties tokenProperties;
public String getToken(){
return "host:"+tokenProperties.getTokenRedisHost()+";pwd:"+tokenProperties.getTokenRedisPwd();
}
}
package com.mayikt.config;
import com.mayikt.utils.TokenProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(TokenProperties.class)
public class TokenAutoConfiguration {
private final TokenProperties tokenProperties;
public TokenAutoConfiguration(TokenProperties tokenProperties){
this.tokenProperties=tokenProperties;
}
@Bean
@ConditionalOnMissingBean
public TokenService tokenService(){
return new TokenService();
}
}
5、mvn clean install
<mainClass>com.mayikt.tokenredisspringbootstarter.TokenRedisSpringbootStarterApplication</mainClass>
<skip>true</skip>
6、xxxx-starter.jar 引用
token-redis-springboot-starter-0.0.3.jar
6.1、pom.xml
<dependency>
<groupId>com.maykit</groupId>
<artifactId>token-redis-springboot-starter</artifactId>
<version>0.0.3</version>
</dependency>
6.2、application.properties
mayikt.token-redis-host=127.0.0.1
mayikt.token-redis-pwd=root
6.3、Contoller
@RestController
public class testContoller {
@Autowired
TokenService tokenService;
@RequestMapping("/hello")
public String getToken(){
return tokenService.getToken();
}
}