puzhibing
2026-03-25 00d8a32a56da77bf4c6267ea2617d4bc15950587
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
package com.xiaopeng.common.core.utils;
 
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
 
@Component
public class ConfigUtils {
 
 
    private static Environment env;
 
    @Autowired
    public void setEnv(Environment environment) {
        ConfigUtils.env = environment;
    }
 
    public static String getProperty(String key) {
        if (env == null) {
            throw new IllegalStateException("Environment not injected yet");
        }
        return env.getProperty(key);
    }
 
    public static String getProperty(String key, String defaultValue) {
        if (env == null) {
            return defaultValue;
        }
        return env.getProperty(key, defaultValue);
    }
 
}