分层应用程序上下文和标准范围和自定义范围

时间:2016-12-09 10:56:01

标签: spring memory-leaks hierarchical session-scope conversation-scope

我的问题有点复杂:)我会尝试解释我的设置......

  • 我有网络应用程序,一个带有一些单例,会话范围的bean的web-spring-context,有时候是aop-proxies等。正常的东西:)
  • 在应用程序内部我创建了一些基础设施,以便能够启动"一些子上下文,我手动执行,单独定义的spring-configuration-classes,这些类是从toplevel / main spring-context-bean-scan中排除的,我有一个未定义的这样的子上下文的数量。 app ...
  • 具有子上下文的功能是可选的,因此应用程序也可以仅使用主上下文
  • 运行
  • 在主要上下文和子上下文中我使用了一些类/接口 - 来分离"顶级"来自"子组件级" bean我创建了两个额外的限定符注释...使用这个注释我拉扯了自动装配的东西,以便我认为正确的bean在正确的位置使用:)
  • 想象在每个子上下文中有顶级FooBarService和N FooBarService,每个服务必须拥有他自己的" state,所以在整个应用程序中运行N + 1个实例
  • 一些子上下文bean正在使用(注入)顶级bean等。
  • 到目前为止似乎有效

  • 但现在我调查了一个大内存泄漏:(我想我发现了问题...

  • 我创建了自己的"会话范围的子组件"实现(hash-map,没有真正的清理等等。 urgs )并且我通过" CustomScopeConfigurer"注册了这个范围。 as" SESSION"对于我的每个(手动启动的)子上下文......有了这个,我仍然可以使用" normal"我的子上下文spring-configuration类中的@Scope(value = WebApplicationContext.SCOPE_SESSION)方法

Q1:这是"手工制作"范围好主意? :d Q2:这个"手工制作"范围应该是一种转换范围"在"会话范围内"但请记住,这个范围在"会话"中存在N次,所以我们称之为" conversation-1-scope"," conversation-2-scope" ......"对话-N-scope" 问题3:何时以及如何清理这样一个"会话范围"?

  • 我期望从一个分层的弹簧上下文中得到一个未在"子上下文中找到的bean"将从父上下文请求(尝试) - 这是对的吗?

问题4:是否有必要注册来自" parent-context"的所有已知范围。在"子背景"像这样:

 final ConfigurableListableBeanFactory parentBeanFactory = ((AbstractApplicationContext) parentSpringContext).getBeanFactory();
  final ConfigurableListableBeanFactory beanFactory = springApplicationContext.getBeanFactory();
  final String[] scopeNames = parentBeanFactory.getRegisteredScopeNames();
  for (final String scopeName : scopeNames) {
    final Scope scope = parentBeanFactory.getRegisteredScope(scopeName);
    beanFactory.registerScope(scopeName, scope);
  }

这是我"结构的可视化":

TOP-LEVEL-Context
 |-* session-scoped beans
 |-* singleton beans (HelloWorldService)
 |
 |->SUB1-Context
 |  |-* session-scoped beans (FooBarService - depends on HelloWorldService)
 |  |-* singleton beans
 |
 |->SUB2-Context
 |  |-* session-scoped beans (FooBarService - depends on HelloWorldService)
 |  |-* singleton beans
 |
 |->...

任何暗示都很棒:)

亲切的问候 安德烈亚斯

1 个答案:

答案 0 :(得分:0)

Are you looking for WeakHashMap by any chance...?

I have no idea of all the Spring crap :) - but the aforementioned thing comes in handy when I need to create and later automatically release subgraphs when using Dagger2.