2017-05-11 49 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"); 
    } 
} 

个人而言,我更喜欢第二个。

相关问题