添加pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class StaticContext { private static AppConfig appConfig; @Autowired public StaticContext(AppConfig appConfig) { StaticContext.appConfig = appConfig; } public static String getAppUrl() { return appConfig.getUrl(); } }
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "sonic") @Data public class AppConfig { private String url; }
最后直接使用
String appUrl = StaticContext.getAppUrl();