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

When to use BeanDefinition.SCOPE_PROTOTYPE vs. ConfigurableBeanFactory.SCOPE_PROTOTYPE [SPR-15342] #19905

Closed
spring-projects-issues opened this issue Mar 11, 2017 · 3 comments
Assignees
Labels
type: task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Mar 11, 2017

Manuel Jordan opened SPR-15342 and commented

Hello Spring team

I have tested that these three ways are valid:

@Scope("prototype")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Of course the first is the most obvious, but about the other two ways: I did a research through the javadoc/api and current Reference Documentation for these two interfaces and there is no mention about when an approach should be used over the other.

Even when it is trivial I think is fair give some indication.


Affects: 4.3 GA, 4.3.6

@spring-projects-issues spring-projects-issues added the type: documentation label Jan 11, 2019
@vananiev
Copy link
Contributor

@vananiev vananiev commented Nov 23, 2019

I also analyzed javadoc/api. I found that "prototype" and ConfigurableBeanFactory.SCOPE_PROTOTYPE may be used with the @Scope annotation, while BeanDefinition.SCOPE_PROTOTYPE is preferably used when definining scope in code by BeanDefinitionBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE) method.

This information is available in org.springframework.beans.factory.config.Scope
org.springframework.context.annotation.Scope java docs (in web version and more obvious in source code).

@sbrannen , is this information enough or is it worth starting a PR?

@sbrannen sbrannen changed the title Improve documentation about when use BeanDefinition.SCOPE_PROTOTYPE or ConfigurableBeanFactory.SCOPE_PROTOTYPE [SPR-15342] Improve documentation about when to use BeanDefinition.SCOPE_PROTOTYPE vs. ConfigurableBeanFactory.SCOPE_PROTOTYPE [SPR-15342] Nov 24, 2019
@sbrannen
Copy link
Member

@sbrannen sbrannen commented Nov 24, 2019

Since the scopeName attribute of the @Scope annotation clearly references ConfigurableBeanFactory.SCOPE_PROTOTYPE and ConfigurableBeanFactory.SCOPE_SINGLETON, I don't think there is any need for further clarifying documentation regarding the constants.

In addition, all that really matters is that the @Scope annotation be supplied a constant expression that evaluates "prototype" for a prototype-scoped bean. Thus, @Scope("prototype") is also perfectly valid (as previously pointed out), but some people prefer not to use String literals for fear of making a typo and may therefore prefer the use of a constant.

Please keep in mind that BeanDefinition.SCOPE_PROTOTYPE does not redefine the constant value but rather simply references ConfigurableBeanFactory.SCOPE_PROTOTYPE.

When working programmatically with bean definitions, one may prefer to reference the BeanDefinition.SCOPE_PROTOTYPE constant, but in the end it's really just a matter of preference.

In any case, I'll tidy up our internal use of those constants for greater consistency.

@sbrannen sbrannen removed this from the General Backlog milestone Nov 24, 2019
@sbrannen sbrannen added this to the 5.2.2 milestone Nov 24, 2019
@sbrannen sbrannen self-assigned this Nov 24, 2019
@sbrannen sbrannen added type: task and removed type: documentation labels Nov 24, 2019
@sbrannen sbrannen changed the title Improve documentation about when to use BeanDefinition.SCOPE_PROTOTYPE vs. ConfigurableBeanFactory.SCOPE_PROTOTYPE [SPR-15342] When to use BeanDefinition.SCOPE_PROTOTYPE vs. ConfigurableBeanFactory.SCOPE_PROTOTYPE [SPR-15342] Nov 24, 2019
@sbrannen
Copy link
Member

@sbrannen sbrannen commented Nov 24, 2019

As a side note, it's also possible to create your own custom "prototype" scope annotation as follows.

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public @interface Prototype {
}

or

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public @interface PrototypeScope {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task
Projects
None yet
Development

No branches or pull requests

3 participants