2017-05-11 46 views
1

我想在jHipster應用程序啓動後執行方法。我應該在哪裏放我的方法? 我試圖在MyApp.java方法運行我的方法:jHipster應用程序啓動後的執行方法

@PostConstruct 
    public void initApplication() 

但我得到的錯誤:

Invocation of init method failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xxx.xxx.xxx 
xxx.xxx.xxx.cars, could not initialize proxy - no Session 

回答

0

Jhipster基於SpringBoot的後端因此該解決方案可能是添加的方法在SpringBoot配置主,就像在這個環節中描述:stack overflow question

萬一解決被刪除,這裏是代碼:

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class Application extends SpringBootServletInitializer { 

    @SuppressWarnings("resource") 
    public static void main(final String[] args) { 
     ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); 

     context.getBean(Table.class).fillWithTestdata(); // <-- here 
    } 
} 

如果您不希望它被阻止,可以使用@Async註釋您調用的方法。

希望這會有所幫助!不要猶豫,要求更多的細節。

+0

我嘗試了您的解決方案,但仍然出現相同的錯誤。 – Ice

+0

也許如果你編輯你的問題,並添加更多的細節,我將能夠提供幫助。 @gaelmarziou答案對你有幫助嗎? – matthieusb

3

你應該定義你註釋一個單獨的類要麼@Service@Component@Configuration取決於你想要達到並注入到這個類,你需要初始化你的數據倉庫JPA什麼。

此課程還可以實施ApplicationRunner interface

或者,您可以使用Liquibase遷移CSV文件考慮加載數據,請參見src/main/resources/config/liquibase/changelog/00000000000000_initial_schema.xmlusers.csv爲例

0

你有兩個選擇。

第一個選項:讓您的主類實現CommandLineRunner。

public class MyJhipsterApp implements CommandLineRunner{ 
    public static void main(String[] args) throws UnknownHostException { 
    //jhipster codes ... 
    } 

    //method implemented from CommandLineRunner 
    @Override 
    public void run(String... strings) throws Exception { 
     log.info("hello world, I have just started up"); 
    } 
} 

第二個選項:創建一個配置文件並偵聽ApplicationReadyEvent來激發您的方法。

@Configuration 
public class ProjectConfiguration { 
    private static final Logger log = 
    LoggerFactory.getLogger(ProjectConfiguration.class); 

    @EventListener(ApplicationReadyEvent.class) 
    public void doSomethingAfterStartup() { 
    log.info("hello world, I have just started up"); 
    } 
} 

個人而言,我更喜歡第二個。

相關問題