大延迟直到SpringApplication.run()方法完成

时间:2019-04-02 13:31:10

标签: java spring spring-boot spring-bean postconstruct

我有以下课程:

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

在同一个项目的另一个班级中,我有:

@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()方法这么晚才能完成?有什么想法吗?

1 个答案:

答案 0 :(得分:1)

启动速度明显较慢的一个原因是SecureRandom的默认实现,该实现扫描网络接口以提供其他系统熵源。

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

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

相关问题