java - SpringApplication.run() 方法完成之前有很大的延迟

标签 java spring spring-boot spring-bean postconstruct

我有以下类(class):

package org.edgexfoundry.pkg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
@EnableDiscoveryClient
public class Application {
  public static ConfigurableApplicationContext ctx;

  public static void main(String[] args) {
    ctx = SpringApplication.run(Application.class, args);
    System.out.println("WELCOME!");
  }

在同一项目的另一个类(class)中,我有:

@ImportResource("spring-config.xml")
public class BaseService {
    @PostConstruct
    private void postConstructInitialize() {
        logger.debug("post construction initialization");
    }
}

我的 spring-config.xml 文件位于:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder
        location="classpath:*.properties" />

    <bean class="org.edgexfoundry.pkg.HeartBeat" />
    <context:component-scan base-package="org.edgexfoundry.pkg" />
    <context:component-scan base-package="org.edgexfoundry" />

</beans> 

HeartBeat 类:

@EnableScheduling
public class HeartBeat {

    private static final EdgeXLogger logger = EdgeXLoggerFactory.getEdgeXLogger(HeartBeat.class);

    @Scheduled(fixedRateString = "${heart.beat.time}")
    public void pulse() {
        logger.info("Beating...");
    }
}

当我运行该项目时,我得到以下输出:

BySpringCGLIB$$1088c4ff] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
/*******************************************************************************
 * Copyright 2017, Dell, Inc.  All Rights Reserved.
 ******************************************************************************/
WELCOME!

2019-04-02 12:50:24.574  INFO 1 --- [           main] org.edgexfoundry.pkg.Application         : No active profile set, falling back to default profiles: default

经过很长一段时间(大约 8 分钟),我得到了最后一行:

2019-04-02 12:57:04.611 DEBUG 1 --- [           main] org.edgexfoundry.pkg.BaseService         : post construction initialization

同时正在做什么?为什么 @PostConstruct 方法需要这么多时间来运行,即为什么 SpringApplication.run() 方法完成得这么晚?有什么想法吗?

最佳答案

启动速度明显缓慢的原因之一是 SecureRandom 的默认实现它扫描网络接口(interface)以提供额外的系统熵源。

可以通过注册自己的 java.security.Provider 来避免这种情况或使用 SecureRandomSpi .

其中还有 this good introduction在 Java 中(快速)安全的伪随机数生成主题中。

关于java - SpringApplication.run() 方法完成之前有很大的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55476084/

相关文章:

java - Vaadin Spring Boot 找不到 npm 包

java - 使用循环确定电子邮件地址是否包含 2 个字符

java - 如何在spring mongotemplate中存储mongodb聚合查询结果?

java - 如何处理实体中的任何字段更改(Spring Boot)

java - Java Spring Boot 的 Docker 基础镜像 (`FROM` 是什么?

Spring 云侦探与 Spring 数据 jpa

java - 如何仅使用关联对象的外键来保存 Spring 实体

java - 进度条未在 fragment 中显示

java - 如何组织类(class)、包

java - 在 Java 中,当评估构造函数调用的参数抛出异常时会发生什么?