在静态函数中使用配置属性

问题描述 投票:0回答:1

我有一个 Spring Boot 应用程序,想要在使用配置属性的

static
中调用
@Services
函数。比如说,我有一个属性类

@Component
@ConfigurationProperties(prefix = "connection")
public class ConnectConfig {
    private String useragent;
    private String referrer;
}

和一个函数

public class MyUtils {
    public static Document connect(String url) throws IOException {
        return Jsoup.connect(url).userAgent("ConnectConfig.useragent").referrer("ConnectConfig.referrer").execute().parse();
    }
}

我怎样才能在那里拥有房产价值?

java spring-boot
1个回答
0
投票

您可以创建一个配置类,并在同一类的 PostContruct() 方法中,您可以调用静态方法 (MyUtils.connect()) 来加载属性。这样,属性将仅在应用程序启动时加载。

© www.soinside.com 2019 - 2024. All rights reserved.