jHipster应用程序启动后执行方法

时间:2017-05-11 00:10:01

标签: java jhipster

我想在jHipster应用程序启动后执行一个方法。我应该把我的方法放在哪里? 我试图在MyApp.java方法中运行我的方法:

    @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

3 个答案:

答案 0 :(得分:3)

您应该定义一个单独的类,使用$ docker network inspect testnet | grep IPv4 "IPv4Address": "172.18.0.2/16", "IPv4Address": "172.18.0.3/16", @Service@Component进行注释,具体取决于您要实现的目标,并将您需要初始化的JPA存储库注入此类你的数据。

此课程还可以实施ApplicationRunner interface

或者,您可以考虑使用Liquibase迁移从CSV文件加载数据,有关示例,请参阅@Configurationsrc/main/resources/config/liquibase/changelog/00000000000000_initial_schema.xml

答案 1 :(得分:0)

Jhipster基于SpringBoot用于后端,因此解决方案可能是在SpringBoot配置main中添加该方法,如此链接中所述: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 注释您正在调用的方法。

希望这有帮助!不要犹豫,询问更多细节。

答案 2 :(得分: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");
  }
}

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