功能:模拟从redis 中获取token ,(不真正的链接redis 只是生成一个简单的tokn)
首先建立一个项目 如下图所示
下边是我的所有maven 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 当别人在引入你的jar包时候 可以在配置文件中给他们提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
1 TokenAutoConfiguration 类
package com.jxd.token.config;
import com.jxd.token.service.TokenService;
import com.jxd.token.utils.TokenProperties;
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 {
@Bean
public TokenService tokenService(){
return new TokenService();
}
}
2 TokenService 类
package com.jxd.token.service;
import com.jxd.token.utils.TokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class TokenService {
@Autowired
private TokenProperties tokenProperties;
public String getToken(){
System.out.println("模拟生成Token");
return tokenProperties.getTokenRedisHost()+","+tokenProperties.getTokenRedisPwd();
}
}
3 TokenProperties 类
package com.jxd.token.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "jxd")
public class TokenProperties {
private String tokenRedisHost;
private String tokenRedisPwd;
public String getTokenRedisHost() {
return tokenRedisHost;
}
public String getTokenRedisPwd() {
return tokenRedisPwd;
}
public void setTokenRedisPwd(String tokenRedisPwd) {
this.tokenRedisPwd = tokenRedisPwd;
}
public void setTokenRedisHost(String tokenRedisHost) {
this.tokenRedisHost = tokenRedisHost;
}
还要在resources 文件下建立一个Directory, META-INF 建立一个 文件spring.factories 把下边这句话 复制到文件中
#spring容器在启动的时候会默认读取META-INF下的文件 同时会把你指定的文件加载到spring容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jxd.token.config.TokenAutoConfiguration
把这个项目达成jar 包 mvn clean install
然后 用这个命令 注入到 你本地的mvn 仓库中
mvn install:install-file -Dfile=token-redis-springboot-starter.jar -DgroupId=com.jxd -DartifactId=token-redis-springboot-starter -Dversion=1.0 -Dpackaging=jar
另起一个 项目 引入刚才的maven 依赖
<dependency>
<groupId>com.jxd</groupId>
<artifactId>token-redis-springboot-starter</artifactId>
<version>1.0</version>
</dependency>
当我们在输入的时候 如下图会提示
这是配置的参数
jxd.token-redis-host= 192.168.1.0.1 jxd.token-redis-pwd= 123456
如下图所示 ,测试出来的结果和我们写的ip 和密码一致
}