カスタムクラスControllerのyml定数で参照されるSpringBoot



Springboot Referenced Yml Constants Custom Class Controller



定数のyml

#カスタム定数
絶え間ない:
TOKEN_EXPIRE_TIME:43200#12時間のトークン期間(秒単位)

1、カスタム定数参照クラスを作成します

package com.coolma.admin.util import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.stereotype.Component /** * @ClassName Constant * @Decription custom constants are accepted in class * @Date 2019/8/21 22:28 * @Version 1.0 **/ @Component @ConfigurationProperties(prefix = 'constant') public class Constant { /** * TOKEN dynamic period */ private Long TOKEN_EXPIRE_TIME public Long getTOKEN_EXPIRE_TIME() { return TOKEN_EXPIRE_TIME } public void setTOKEN_EXPIRE_TIME(Long TOKEN_EXPIRE_TIME) { this.TOKEN_EXPIRE_TIME = TOKEN_EXPIRE_TIME } }

2、リファレンス

package com.coolma.admin.util import java.io.Serializable import java.util.Date import java.util.HashMap import java.util.Map import javax.annotation.PostConstruct import javax.servlet.http.HttpServletRequest import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.stereotype.Component /** * Test test class * @date Nov 20, 2018 */ @Component public class JwtTokenUtils { /** * The first way */ @Autowired private Constant constant // custom constants are accepted in class public static JwtTokenUtils jwtTokenUtils @PostConstruct public void init(){ jwtTokenUtils=this this.constant=jwtTokenUtils.constant } /* * The second way */ //@Value('${constant.TOKEN_EXPIRE_TIME}') //private Long TOKEN_EXPIRE_TIME /** * Generate Token * * @Param userName the user name * @Param userId user id * @Return a token */ public static void test(){ System.Out.Println ( 'Constant value:' '' '' '' '' '' '' '+ jwtTokenUtils.constant.getTOKEN_EXPIRE_TIME ()) } }