【问题标题】:Tomcat 8.5 Wildfly 15 Java 8 OutOfMemoryError and very slow startupTomcat 8.5 Wildfly 15 Java 8 OutOfMemoryError 和非常慢的启动
【发布时间】:2019-08-08 02:22:36
【问题描述】:

在启用@Autowrid 后,使用 Spring 4 的 Maven 项目大约:800 ManegedBeans Spring、900 Services、1000 @Component 和 1000 @Repository,启动应用程序将使用 OutOfMemoryError。 我在 Tomcat 和 Wildfly 中增加了参数-Xms1024m -Xmx4g,在应用程序中我在applicationContext.xml 中添加了default-lazy-init = "false" 参数,并且应用程序正在以很高的成本启动。我想知道删除是否有任何优势

@Autowired 
private AnyService anyService;

来自所有类并使用

public void execute() {
   AnyService anyService = (AnyService)applicationContext.getBean(AnyService.class);
   anyService.execute();
}

在方法中。 使用局部变量而不是实例可以优化启动吗?给GC带来一些好处?调用 applicationContext.getBean(AnyService.class) 可能会降低方法的性能?

【问题讨论】:

    标签: java spring maven out-of-memory wildfly


    【解决方案1】:

    OutOfMemoryError 和启动缓慢是两个不同的问题。我认为您不必删除 @Autowired 注释。

    首先你应该确定是哪个bean导致加载缓慢(例如db连接),并在那个类中添加@Lazy,你可以参考这篇文章spring-lazy-annotation-use-cases。并且一些 bean 应该首先加载 Make sure a particular bean gets initialized first

    其次,使用 Spring XML 配置而不是组件扫描或限制组件扫描范围可能会有所帮助。确保只使用应用程序的最低要求组件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2012-01-24
      • 2011-10-28
      • 1970-01-01
      • 2014-04-04
      相关资源
      最近更新 更多