Skip to content

Doc: Static @Bean methods cannot refer to each other with Spring scoping semantics [SPR-13118] #17709

@spring-projects-issues

Description

@spring-projects-issues

Johannes Zick opened SPR-13118 and commented

In our project, we use a custom Scope that can be controlled to some degree by the application. The Bean is declared in a @Configuration class using the @Bean annotation. The same class contains the CustomScopeConfigurer declaration referencing the Scope by method name.

@Configuration
public class Configuration
{
    @Bean
    static CustomScope getCustomScope()
    {
        return new CustomScope();
    }

    @Bean
    static CustomScopeConfigurer getCustomScopeConfigurer()
    {
        CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
        customScopeConfigurer.addScope("customScope", getCustomScope());
        return customScopeConfigurer;
    }
}

To control the custom Scope, we tried to inject it using @Autowired. This resulted in a new Bean being created. To access the Bean being used internally, we have to inject the ApplicationContext, retrieve the BeanFactory, then the registered Scope.

Usually, one would expect to have easy access to the custom Scope, since it is declared in the same @Configuration class. But apparently it lives in a completely separate Context. I can access it, however, via public methods.

I would highly recommend to change this, and add a word or two to the Javadocs. Either have it in a separate Context and not accessible, or allow access and make it injectable. The current behavior is inconsistent and not documented, as far as I can tell.


Affects: 4.1.5

Issue Links:

Referenced from: commits f58e1db

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions