简体   繁体   English

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

[英]Hierarchical Application Context and Standard-Scopes and Custom-Scopes

my question is a little bit complicated :) I will try to explain my setup ... 我的问题有点复杂:)我将尝试解释我的设置...

  • I have web-application, one web-spring-context with some singleton-, session-scoped beans, sometimes aop-proxies etc. normal stuff :) 我有一个Web应用程序,一个Web Spring上下文,带有一些单例,会话作用域的bean,有时是aop代理等。
  • inside the app I create some infrastructure to be able to "start" some sub-contexts, I do this manually, separately defined spring-configuration-classes, these classes are exclude from the toplevel/main spring-context-bean-scan, I have an undefined number of such sub context's in the app ... 在应用程序内部,我创建了一些基础结构以能够“启动”某些子上下文,我手动进行了此操作,分别定义了spring-configuration类,这些类不包括在顶层/主spring-context-bean-scan中,在应用程序中有未定义数量的此类子上下文...
  • the feature with sub-contexts is optional, so the app can also run only with the main context 具有子上下文的功能是可选的,因此该应用程序也只能在主上下文中运行
  • inside the main context and the sub-context's I'm using some classes/interfaces - to separate "top-level" from "sub-component-level" beans I created two additional Qualifier Annotations ... with this annotations I pimped the autowiring stuff so that I think the right bean is used at the right place :) 在主上下文和子上下文的内部,我正在使用一些类/接口-将“顶层”和“子组件级” bean分开,我创建了两个附加的Qualifier注释...通过此注释,我将自动装配的东西,以便我认为在正确的地方使用正确的bean :)
  • imagine there is FooBarService on toplevel and N FooBarService's in each sub-context, each Service must have his "own" state, so N+1 instances running in the whole application 想象在顶层有FooBarService,在每个子上下文中有N个FooBarService,每个Service必须具有自己的“拥有”状态,因此N + 1个实例在整个应用程序中运行
  • some sub-context beans are using (inject) top-level beans etc. 一些子上下文Bean正在使用(注入)顶级Bean等。
  • this seems to work so far 这似乎到目前为止

  • but now I investigated a BIG memory leak :( I think I found the problem ... 但是现在我调查了一个大的内存泄漏:(我想我发现了问题...

  • for the sub-components I created my own "session-scoped" implementation (hash-map, no real cleanup etc. urgs ) and I registered this scoped via "CustomScopeConfigurer" as "SESSION" for each of my (manually started) sub-contexts ... with this I can still use the "normal" @Scope(value = WebApplicationContext.SCOPE_SESSION) approach in my sub-context spring-configuration classes 对于子组件,我创建了自己的“会话范围”实现(哈希映射,没有真正的清理等。urgs ),并通过“ CustomScopeConfigurer”将其范围注册为每个(手动启动)子组件的“ SESSION”。上下文...这样,我仍然可以在子上下文弹簧配置类中使用“常规” @Scope(value = WebApplicationContext.SCOPE_SESSION)方法

Q1: is this "handmade" scope a good idea? Q1:这个“手工”示波器是个好主意吗? :D Q2: this "handmade" scope should be a kind of "conversion-scope" inside a "session-scope" but keep in mind this scope exists N times in a "session", so lets call it "conversation-1-scope", "conversation-2-scope" ... "conversation-N-scope" Q3: when and how can I cleanup such a "conversation-scope"? :D Q2:此“手工”范围应该是“会话范围”内的一种“转换范围”,但请记住,此范围在“会话”中存在N次,因此我们将其称为“会话-1”范围”,“ conversation-2-scope” ...“ conversation-N-scope” Q3:何时以及如何清除此类“ conversation-scope”?

  • I expected from a hierarchical spring-context that a bean which is not found in a "sub-context" would be requested (try) from the parent-context - is this right? 我从分层的Spring上下文中期望,将在父上下文中请求(尝试)在“子上下文”中找不到的bean-是这样吗?

Q4: is it necessary to register all the known scopes from "parent-context" in the "sub-context" like this: 问题4:是否有必要像这样在“子上下文”中注册“父上下文”中的所有已知范围:

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

Here is a visualization of my "structure": 这是我的“结构”的可视化:

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
 |
 |->...

Any hint would be awesome :) 任何提示都很棒:)

Kind regards Andreas 问候安德烈亚斯

Are you looking for WeakHashMap by any chance...? 您是否正在寻找WeakHashMap ...?

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. 我不知道所有的Spring废话:)-但是当我需要创建并稍后在使用Dagger2时自动释放子图时,上述操作很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM