【问题标题】:Compute the time taken to load spring beans计算加载 spring bean 所花费的时间
【发布时间】:2016-09-12 23:56:59
【问题描述】:

我有一个由 100 多个 spring.xml 文件组成的应用程序。我想弄清楚加载这些spring.xml 文件中定义的所有beans 所花费的时间。请你让我知道如何做到这一点?谢谢!

【问题讨论】:

标签: java spring dependency-injection


【解决方案1】:

long then = System.currentTimeMillis();

// 做你所有的事情

System.out.println("花费的毫秒数:" + System.currentTimeMillis() - then);

【讨论】:

  • @PunterVicky 无论您想在哪里计时,都没有那么复杂。您只是标记一个起点,然后从当前时间中减去它。
  • 谢谢,但我不确定必须在哪里更新才能计算 spring bean 的加载时间。
【解决方案2】:

您可以使用测试类计算 spring bean 加载时间。手动加载所有 xml 文件(不使用注释 - @ContextConfiguration)。

@Before
public void setup(){
    long then = System.currentTimeMillis();
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "com/abc/applicationContext.xml",
            "com/abc/service-context.xml");
    long now = System.currentTimeMillis();
    System.out.println("Difference is :"+(now - then));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多