Jonathan Ferreira
Jonathan Ferreira

Reputation: 83

What is the difference between @Scope (BeanDefinition.SCOPE_PROTOTYPE) and @Scope (“prototype”)?

Does the annotation @Scope(BeanDefinition.SCOPE_PROTOTYPE) have any advantages or differences compared to the annotation @Scope("prototype")?

Upvotes: 2

Views: 1138

Answers (2)

Juan Moreno
Juan Moreno

Reputation: 2785

Both expressions are equivalents, BeanDefinition.SCOPE_PROTOTYPE is a constant in the ConfigurableBeanFactory interface with the text "prototype" that is used in the BeanDefinition interface:

/**
 * Scope identifier for the standard prototype scope: {@value}.
 * <p>Note that extended bean factories might support further scopes.
 * @see #setScope
 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
 */
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

As suggested @santossh-kumhar, the main advantage is that using the constant SCOPE_PROTOTYPE instead of the text "prototype" is more typo safe.

Upvotes: 0

Brooklyn99
Brooklyn99

Reputation: 1047

They do the same thing meaning you can interchange them and shows no difference in behavior.

Does the annotation @Scope(BeanDefinition.SCOPE_PROTOTYPE) have any advantages?

Advantage I can think of are when you use the pre-defined constants is you have an advantage of avoiding typos and saving your time instead of declaring string literals.

Upvotes: 1

Related Questions