puzhibing
2026-03-25 e56d7aa26b44e519be4367cd56df50a75f3ac66f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.xiaopeng.common.core.utils;
 
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
 
@Component
public class ConfigUtils {
 
 
    // 1. 正常注入非静态字段
    @Value("${token.secret}")
    private String secret;
 
 
    private static ConfigUtils instance;
 
    @PostConstruct
    public void init() {
        System.err.println("ConfigUtils init");
        instance = this;
    }
 
    // 3. 提供静态方法供外部调用
    public static String getMyConfigValue() {
        if (instance == null) {
            throw new IllegalStateException("ConfigUtils has not been initialized by Spring yet.");
        }
        return instance.secret;
    }
 
    // 如果需要修改值,也可以提供静态 setter (视需求而定)
    public static void setMyConfigValue(String value) {
        if (instance != null) {
            instance.secret = value;
        }
    }
}