Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: @Scope not inherited from base class [SPR-16602] #21143

Closed
spring-projects-issues opened this issue Mar 16, 2018 · 3 comments
Closed

Doc: @Scope not inherited from base class [SPR-16602] #21143

spring-projects-issues opened this issue Mar 16, 2018 · 3 comments
Assignees
Labels
status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Max Huang opened SPR-16602 and commented

Bean definition inheritance documentation states the following:

A child bean definition inherits scope, constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any scope, initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings.

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, lazy init.

Based on the above statement, the scope of the child bean below should be prototype, however, it shows the child's scope as singleton.

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public abstract class Parent {

}


@Component
public class Child extends Parent {

}

@Component
public class CustomBeanFactory implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println(beanFactory.getBeanDefinition("child").getScope()); // displays singleton
    }
}

Is the above behavior due to the the child bean takes in the default scope (which is a singleton) and overrides the parent's scope?


Affects: 4.3.14, 5.0.4

Referenced from: commits 914b247, 8818176

Backported to: 4.3.15

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

That documentation section actually talks about "bean definition inheritance" as in the XML parent attribute and corresponding programmatic setters on GenericBeanDefinition where a scope declaration may indeed get inherited. I'm afraid this is orthogonal to class hierarchies, and our annotations (including @Scope) generally follow their own rules: not really exposing bean metadata inheritance to begin with but rather always creating an individual bean definition for the concrete component class. I'll put some explicit notes into the documentation about this.

@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches type: documentation A documentation task labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0.5 milestone Jan 11, 2019
@pnandagopal
Copy link

@jhoeller what are you trying to say here. Your comment is not clear either.

@sbrannen
Copy link
Member

Juergen was pointing out that the @Scope annotation is not meta-annotated with @java.lang.annotation.Inherited and is therefore not inherited by subclasses. Thus, although there is a parent-child relationship of sorts between superclasses and subclasses, @Scope is not inherited.

This is in contrast to parent and child bean definitions in Spring XML configuration files, and the original author of this issue copied a section from the reference manual referring to XML configuration which does not apply to annotation-based configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants