现在想要做的事情是在DownLoadUtils工具类中,读取到设置启动类中的环境变量。
操作是
1. 将 class DownLoadUtils 上添加 @Component 声明其为bean组件。
2. 使用 @Autowired注入启动类。
3. 在 DownLoadUtils 中声明一个 静态的私有变量 private static DownloadUtils downloadUtils;
4. 添加共有的init方法,并用@PostConstruct声明 在项目启动的时候进行执行该方法,也可以理解为在 spring 容器初始化的时候执行该方法。
public void init(){
downloadUtils = this;
downloadUtils.logctApplicationConfig = this.logctApplicationConfig;
}
整体代码为
@Component
public class DownloadUtils {
private DownloadUtils(){}
private static final Logger logger = LoggerFactory.getLogger (DownloadUtils.class);
@Autowired
public LogctApplicationConfig logctApplicationConfig;
private static DownloadUtils downloadUtils;
@PostConstruct
public void init(){
downloadUtils = this;
downloadUtils.logctApplicationConfig = this.logctApplicationConfig;
}
本文介绍如何在Spring项目中通过DownLoadUtils工具类读取启动类中的环境变量。具体步骤包括将DownLoadUtils声明为bean组件,使用@Autowired注解注入启动类,在DownLoadUtils中声明静态私有变量并定义@PostConstruct注解的方法来完成初始化。

被折叠的 条评论
为什么被折叠?



