2015-12-21 55 views
0

我想在我的Spring Boot JPA MVC Web應用程序中填充一些初始/示例記錄。在Grails中,我看到一個樣本,其中在grails-app/init/BootStrap文件中完成了這種初始化,在init中關閉。Spring Boot中的Grails'BootStrap#init等價物嗎?

如何在Spring Boot web應用程序中執行相同的操作?

回答

1

這沒什麼特別的。只需爲初始化創建一個類並將其聲明爲spring bean。像典型的spring bean一樣聲明您的依賴關係,如存儲庫,記錄器,事務等。

Spring對聲明將bean創建後,被稱爲像postConstruct方法,afterPropertiesSetinit-method等,您可以使用到這些脫掉你的初始化幾種方法。欲瞭解更多關於初始化回調http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lifecycle-initializingbean

這裏是一個不錯的博客與例如https://coderwall.com/p/wfmxga/bootstrapping-data-with-spring

+0

令人難以置信的比賽,謝謝! – Dims

+0

歡迎您:) –