我需要调用其他应用程序的其余API调用来获取属性,这些属性可以在应用程序级别使用。
这需要在应用程序启动期间准备好供整个应用程序使用。 例如,我们有PropertyPlaceholderConfigurer,它可以包含在应用程序上下文中。但是我们不想在这里包含这些属性。
是否有其他类会在上下文启动期间初始化应用程序?
谢谢, KK
答案 0 :(得分:0)
您可以使用CommandLineRunner
例如
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
然后使用RestTemplate进行调用
RestTemplate restTemplate = new RestTemplate();
答案 1 :(得分:0)
您可以创建ApplicationListener。在Spring上下文初始化之后,此方法可用于运行逻辑。
为了实现这一点,您需要创建一个实现ApplicationListener接口的bean:
@Component
public class StartupApplicationListenerExample implements
ApplicationListener<ContextRefreshedEvent> {
private static final Logger LOG = Logger.getLogger(StartupApplicationListenerExample.class);
public static int counter;
@Override public void onApplicationEvent(ContextRefreshedEvent event) {
LOG.info("Increment counter");
counter++;
}
}
答案 2 :(得分:0)
使用@PostConstruct
注释注释方法并将逻辑放在其中调用REST Api。
答案 3 :(得分:0)
如果您只收到第三个应用程序中的属性,则可能应该查看Spring Cloud Config(https://cloud.spring.io/spring-cloud-config/)。
使用此项目,您可以设置一个应用程序,该应用程序仅向所有要求的人提供属性。这些属性可以存储在git或任何你喜欢的地方。
将此作为属性提供程序运行,您可以从那里检索属性并将其与本地属性合并。 这一切都是在app-startup上完成的。