spring中应用程序启动期间的RESTAPI调用

spring中应用程序启动期间的RESTAPI调用,spring,spring-mvc,Spring,Spring Mvc,我需要调用RESTAPI调用其他应用程序来获取属性,这些属性可以在应用程序级别使用 这需要在应用程序启动期间完成,并准备好供整个应用程序使用。 例如,我们有PropertyPlaceHolderConfigure,它可以包含在应用程序上下文中。但是我们不想在这里包含属性 在上下文启动期间是否有其他类将初始化应用程序 谢谢, Kk您可以使用CommandLineRunner 范例 @Component public class MyBean implements CommandLineRunner

我需要调用RESTAPI调用其他应用程序来获取属性,这些属性可以在应用程序级别使用

这需要在应用程序启动期间完成,并准备好供整个应用程序使用。 例如,我们有PropertyPlaceHolderConfigure,它可以包含在应用程序上下文中。但是我们不想在这里包含属性

在上下文启动期间是否有其他类将初始化应用程序

谢谢,
Kk

您可以使用CommandLineRunner

范例

@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
    // Do something...
}
}
然后使用RestTemplate调用
RestTemplate RestTemplate=new RestTemplate()

您可以使用CommandLineRunner

范例

@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
    // Do something...
}
}
然后使用RestTemplate调用
RestTemplate RestTemplate=new RestTemplate()

您可以创建一个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++;
  }
}
@组件
公共类StartupApplicationListenerExample实现
应用程序侦听器{
私有静态最终记录器LOG=Logger.getLogger(StartupApplicationListenerExample.class);
公共静态计数器;
@覆盖ApplicationEvent上的公共无效(ContextRefreshedEvent事件){
日志信息(“增量计数器”);
计数器++;
}
}

您可以创建一个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++;
  }
}
@组件
公共类StartupApplicationListenerExample实现
应用程序侦听器{
私有静态最终记录器LOG=Logger.getLogger(StartupApplicationListenerExample.class);
公共静态计数器;
@覆盖ApplicationEvent上的公共无效(ContextRefreshedEvent事件){
日志信息(“增量计数器”);
计数器++;
}
}

使用
@PostConstruct
注释对方法进行注释,并将调用REST Api的逻辑放入其中。

使用
@PostConstruct
注释对方法进行注释,并将调用REST Api的逻辑放入其中。

如果您仅从第三个应用程序收到属性,您可能应该看看springcloudconfig()

通过这个项目,您可以设置一个应用程序,该应用程序只向提出请求的每个人提供属性。这些属性可以存储在git或任何您喜欢的东西上

将其作为属性提供程序运行,您可以从中检索属性并将其与本地属性合并。
这一切都是在应用程序启动时完成的。

如果您只从第三个应用程序收到您的属性,您可能应该查看Spring Cloud Config()

通过这个项目,您可以设置一个应用程序,该应用程序只向提出请求的每个人提供属性。这些属性可以存储在git或任何您喜欢的东西上

将其作为属性提供程序运行,您可以从中检索属性并将其与本地属性合并。 这一切都是在应用程序启动时完成的