- 1、首先将一个javabean对象使用springboot 的ConfigurationProperties注解将值注入进去,并且注解上component注解。
- 2、在另外一个component注解的util类中,写一个该对象的静态变量,然后写一个构造函数,注入该静态变量,在下面的静态方法中可以直接获取值,而不用再次获取。不需要将javabean对象设置为单例,也不用将属性设置为静态就可以获取。
例子:
javabean对象
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "spring-boot-plus.jwt")
public class JwtProperties {
/**
* 密码
*/
private String secret;
/**
* 签发人
*/
private String issuer;
/**
* 主题
*/
private String subject;
/**
* 签发的目标
*/
private String audience;
/**
* token失效时间,默认30分钟
*/
private Integer expireMinutes;
}
测试类
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
@Component
@Slf4j
public class JwtUtil {
private static JwtProperties jwtProperties;
public JwtUtil(JwtProperties jwtProperties) {
JwtUtil.jwtProperties = jwtProperties;
log.debug(JSON.toJSONString(jwtProperties, true));
}
public static String create() {
DateUtils.addMinutes(new Date(), jwtProperties.getExpireMinutes());
MapUtils.isEmpty(payloadClaims)
jwtProperties.getExpireMinutes();
jwtProperties.getIssuer();
jwtProperties.getSubject();
jwtProperties.getAudience();
return create(null,null,null);
}
}