发布于 1970-01-01 08:00
  • 11 个回答
    • ApplicationReadyEvent实际上只有在您要执行的任务不是正确的服务器操作要求时才有用.启动异步任务以监视某些更改是一个很好的例子.

      但是,如果您的服务器在任务完成之前处于"未就绪"状态,则最好实施,SmartInitializingSingleton因为您将在打开REST端口并且服务器开放营业之前获得回调.

      不要试图@PostConstruct用于只应该发生过一次的任务.当你注意到它被多次调用时,你会得到一个粗鲁的惊喜......

      2022-12-11 02:07 回答
    • 这很简单:

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

      测试版本 1.5.1.RELEASE

      2022-12-11 02:07 回答
    • "Spring Boot"方式是使用a CommandLineRunner.只需添加那种类型的豆子就可以了.在Spring 4.1(Boot 1.2)中,还有SmartInitializingBean一个在所有内容初始化后获得回调.还有SmartLifecycle(从春季开始).

      2022-12-11 02:09 回答
    • 尝试:

      @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
          }
      }
      

      2022-12-11 02:10 回答
    • SmartInitializingSingleton在spring> 4.1中使用bean

      @Bean
      public SmartInitializingSingleton importProcessor() {
          return () -> {
              doStuff();
          };
      
      }
      

      作为替代方案,CommandLineRunner可以实现bean或使用bean注释bean方法@PostConstruct.

      2022-12-11 02:12 回答
    • 为什么不创建一个在初始化时启动监视器的bean,例如:

      @Component
      public class Monitor {
          @Autowired private SomeService service
      
          @PostConstruct
          public void init(){
              // start your monitoring in here
          }
      }
      

      init为bean完成任何自动装配之前,不会调用该方法.

      2022-12-11 02:13 回答
    • 提供一个Dave Syer答案的示例,该示例的工作原理很不错:

      @Component
      public class CommandLineAppStartupRunner implements CommandLineRunner {
          private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
      
          @Override
          public void run(String...args) throws Exception {
              logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
          }
      }
      

      2022-12-11 02:56 回答
    • 配弹簧配置:

      @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");
        }
      }
      

      2022-12-11 02:57 回答
    • 试试这个,当应用程序上下文完全启动时,它将运行您的代码。

       @Component
      public class OnStartServer implements ApplicationListener<ContextRefreshedEvent> {
      
          @Override
          public void onApplicationEvent(ContextRefreshedEvent arg0) {
                      // EXECUTE YOUR CODE HERE 
          }
      }
      

      2022-12-11 03:08 回答
    • 您可以使用扩展类ApplicationRunner,重写该run()方法并在其中添加代码.

      import org.springframework.boot.ApplicationRunner;
      
      @Component
      public class ServerInitializer implements ApplicationRunner {
      
          @Override
          public void run(ApplicationArguments applicationArguments) throws Exception {
      
              //code goes here
      
          }
      }
      

      2022-12-11 03:12 回答
    • 你试过ApplicationReadyEvent吗?

      @Component
      public class ApplicationStartup 
      implements ApplicationListener<ApplicationReadyEvent> {
      
        /**
         * This event is executed as late as conceivably possible to indicate that 
         * the application is ready to service requests.
         */
        @Override
        public void onApplicationEvent(final ApplicationReadyEvent event) {
      
          // here your code ...
      
          return;
        }
      }
      

      代码来自:http://blog.netgloo.com/2014/11/13/run-code-at-spring-boot-startup/

      这是文档提到的关于启动事件的内容:

      ...

      应用程序运行时,应按以下顺序发送应用程序事件:

      ApplicationStartedEvent在运行开始时发送,但在除侦听器和初始化程序注册之外的任何处理之前发送.

      当要在上下文中使用的环境已知但在创建上下文之前,将发送ApplicationEnvironmentPreparedEvent.

      ApplicationPreparedEvent在刷新开始之前发送,但是在加载bean定义之后发送.

      刷新后发送ApplicationReadyEvent,并且已处理任何相关的回调以指示应用程序已准备好为请求提供服务.

      如果启动时发生异常,则发送ApplicationFailedEvent.

      ...

      2022-12-11 03:12 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有