Skip to content

Custom Scope not visible to child context [SPR-7089] #11749

Closed
@spring-projects-issues

Description

@spring-projects-issues

Jack Punt opened SPR-7089 and commented

A Custom Scope (FlexClientScope, in case cited) must be registered with the root applicationContext.

In order to add Beans (a @RemotingDestination) a sub-context is created
as the target for: setParent(rootAppCtx); register(clazz); refresh(); getBean(beanName);
In the case cited, an AnnotationConfigApplicationContext.

When refresh(), the clazz with @Service @RemotingDestination
@Scope(value="flexclient", proxyMode = ScopedProxyMode.TARGET_CLASS)
proceeds to AbstractBeanFactory.java line 320: which attempts to find the requested scope,
but looks only in the current/child ApplicationContext, failing to find the Scope in the parent.

The proposed fix is to add a method (near line 779, after getRegisteredScope()) something like:

protected Scope getScopeUseParent(String scopeName) {
    BeanFactory beanFactory = this;
    Scope scope = this.scopes.get(scopeName);
    while ((scope == null)
           && (beanFactory instanceof HierarchicalBeanFactory)
           && ((beanFactory = ((HierarchicalBeanFactory)beanFactory).getParentBeanFactory()) != null) 
           && (beanFactory instanceof AbstractBeanFactory)) {
        scope = ((AbstractBeanFactory)beanFactory).scopes.get(scopeName);
    }
    return scope;                   // which may be null...
}

And use Scope scope = getScopeUsingParents(scopeName);
instead of: Scope scope = this.scopes.get(scopeName);
In the appropriate places in AbstractBeanFactory.java

[Documentation may need to be upgraded to warn non-AbstractBeanFactory implementations
that they should also search the parents to find registered Scopes.]

Note: for my project this is a "Major" "Blocker" Priority
and requires a source-level patch and class replacement.
but having upgraded the AbstractBeanFactory.class, i'm back in business until next release.
I respect the fact that it is apparently not a common requirement,
(it has gone this long without complaint) but would appreciate a supported solution when possible.

Environmental Impact: The suggested fix is "benign" (has little effect on existing/working apps),
as all of them apparently register and access custom Scopes in the same appCtx.
That is: the new while loop only runs before or in place of existing code that throws an Exception.
So at worst: existing code that relied on not finding a Scope might fail to get an Exception.


Affects: 3.0.2

Reference URL: http://forum.springsource.org/showpost.php?p=294224&postcount=13

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions