现在想要做的事情是在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;
}