2017-08-02 101 views
0

我有一個要求將其餘API調用調用到其他應用程序以獲取屬性,並且這些屬性可以用於應用程序級別。春季應用程序啓動期間的其他API調用

這是在應用程序啓動過程中需要的,可供整個應用程序使用。 例如,我們有PropertyPlaceholderConfigurer,它可以包含在應用程序上下文中。但我們不希望在這裏包含這些屬性。

是否有任何其他類在上下文啓動期間初始化應用程序?

感謝, KK

回答

0

可以使用CommandLineRunner

例如

@Component 
public class MyBean implements CommandLineRunner { 
public void run(String... args) { 
    // Do something... 
} 
} 

,然後使用RestTemplate調用 RestTemplate restTemplate = new RestTemplate();

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++; 
    } 
} 
0

標註與@PostConstruct註釋的方法,並把打電話給你的REST API中它的邏輯。

0

如果您僅收到來自第三方應用程序的屬性,則可能應該查看Spring Cloud Config(https://cloud.spring.io/spring-cloud-config/)。

通過此項目,您可以設置一個應用程序,該應用程序僅向所有詢問的人員提供屬性。這些屬性可以存儲在git或任何你喜歡的。

將此項作爲屬性提供程序運行後,您可以從中檢索屬性並將其與本地屬性合併。 這些都是在應用程序啓動時完成的。

相關問題