【发布时间】:2021-08-19 22:27:59
【问题描述】:
我有一个 Spring Boot 应用程序和两个来自我正在使用的不同 jar 的类,其中一个是 @Component,另一个是 @Configuration。
它们都有@PostConstruct 方法,基本上这是我的用例-> 我希望@Configuration 的@PostConstruct 在@Component 的@PostConstruct 之前运行。有没有可能以某种方式实现这一目标?
我尝试在@Component 上使用@DependsOn(引用@Configuration - 里面没有任何bean - 只有@PostConstruct),但它不起作用。
这是代码片段。
第一个文件:
@Configuration
public class MainConfig {
@PostConstruct
public void postConstruct() {
// doSomething
}
}
第二个文件。
@Component
public class SecondClass {
@PostConstruct
public void init() throws InterruptedException {
// doSomething that depends on postConstruct from MainConfig
}
}
提前非常感谢
【问题讨论】:
-
我从未见过带有后期构造例程的配置对象..
-
@mre 这在技术上是允许的,但相同;没见过。这是混合职责。
-
我对你在
MainConfig::postConstruct所做的事情非常感兴趣。虽然你可以做到,但这可能是非常错误的。
标签: java spring-boot spring-boot-configuration