| | |
| | | package com.xiaopeng.common.core.utils; |
| | | |
| | | |
| | | import com.xiaopeng.common.core.constant.TokenConstants; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | |
| | | @Component |
| | | public class ConfigUtils { |
| | | |
| | | |
| | | // 1. 正常注入非静态字段 |
| | | @Value("${token.secret}") |
| | | @Value("${token.secret:${spring.token.secret:}}") |
| | | private String secret; |
| | | |
| | | |
| | |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | System.err.println("ConfigUtils init"); |
| | | instance = this; |
| | | TokenConstants.setSecret(secret); |
| | | } |
| | | |
| | | // 3. 提供静态方法供外部调用 |
| | | public static String getMyConfigValue() { |
| | | public static String getTokenSecret() { |
| | | if (instance == null) { |
| | | throw new IllegalStateException("ConfigUtils has not been initialized by Spring yet."); |
| | | return TokenConstants.getSecret(); |
| | | } |
| | | return instance.secret; |
| | | } |
| | | |
| | | // 如果需要修改值,也可以提供静态 setter (视需求而定) |
| | | public static void setMyConfigValue(String value) { |
| | | public static void setTokenSecret(String value) { |
| | | if (instance != null) { |
| | | instance.secret = value; |
| | | } |
| | | TokenConstants.setSecret(value); |
| | | } |
| | | } |
| | | |