简体   繁体   中英

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 :)
  • 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 ...
  • 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 :)
  • 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
  • some sub-context beans are using (inject) top-level beans etc.
  • 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

Q1: is this "handmade" scope a good idea? :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"?

  • 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?

Q4: is it necessary to register all the known scopes from "parent-context" in the "sub-context" like this:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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