Scope of a bean means lifecycle of an instance. For example, it can be prototype, a singleton and so forth. In this article, we will see how to set a scope using @Scope
annotation.
A @Scope
annotation is used at the type level or at method level. If at type level then it has to be used in conjunction with either a @Component
or @Configuration
annotation.
If it is at method level, it should be used in conjunction with @Bean
annotation.
By default, if @Scope
is not used, a bean is registered as singleton.
Adding @Scope at type and method level
- Define
@Scope
at type level – SeeDefaultScopedBean
. If we don’t specify any value or use@Scope
, the default is singleton. - Override the default scope – See
PrototypedScopedBean. We have overridden the scope to
ConfigurableBeanFactory.SCOPE_PROTOTYPE which is equal to “prototype”. - Scope used at method level in conjunction with
@Bean
– SeeprototypedBean
. We have overridden the scope to@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
. - A scope define at type level won’t propagate to the
@Bean
methods. SeeMultipleBeansHolder
. It is scoped as prototype. It contains a couple of bean methodsbean1
and
bean1AsSingleton
. We have not added any@Scope
attribute tobean1()
, it doesn't inherit the type's scope rather the default scope kicks in, that is, singleton. There is also a@ComponentScan
. The same rule applied even there.
SpringScopeAnnotationExample:
package com.javarticles.spring.annotations; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; public class SpringScopeAnnotationExample { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); try { ctx.register(DefaultScopedBean.class, PrototypedScopedBean.class, SingleBeanHolder.class, MultipleBeansHolder.class); ctx.refresh(); System.out.println("Is DefaultScopedBean singleton? " + ctx.getBeanDefinition("defaultScoped").isSingleton()); System.out.println("Is prototypedScoped prototype? " + ctx.getBeanDefinition("prototypedScoped").isPrototype()); System.out.println("Is prototypedBean prototype? " + ctx.getBeanDefinition("prototypedBean").isPrototype()); System.out.println("Is multipleBeansHolder prototype? " + ctx.getBeanDefinition("multipleBeansHolder").isPrototype()); System.out.println("Is bean1 prototype? " + ctx.getBeanDefinition("bean1").isPrototype()); System.out.println("Is bean1 singleton? " + ctx.getBeanDefinition("bean1").isSingleton()); System.out.println("Is bean1AsSingleton singleton? " + ctx.getBeanDefinition("bean1AsSingleton").isSingleton()); System.out.println("Is beanA singleton? " + ctx.getBeanDefinition("beanA").isSingleton()); System.out.println("Is beanAAsPrototype prototype? " + ctx.getBeanDefinition("beanAAsPrototype").isPrototype()); } finally { ctx.close(); } } @Configuration("defaultScoped") @Scope static class DefaultScopedBean { } @Configuration("prototypedScoped") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) static class PrototypedScopedBean { } static class SingleBeanHolder { @Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public SomeObject prototypedBean() { return new SomeObject(); } } @Configuration("multipleBeansHolder") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @ComponentScan("com.javarticles.spring.annotations.packageA") static class MultipleBeansHolder { @Bean public SomeObject bean1() { return new SomeObject(); } @Bean @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public SomeObject bean1AsSingleton() { return new SomeObject(); } } static class SomeObject { } }
BeanA:
package com.javarticles.spring.annotations.packageA; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration("beanA") public class BeanA { @Bean @Scope("prototype") public BeanA beanAAsPrototype() { return new BeanA(); } }
Output:
Jan 31, 2016 1:12:30 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.spring[email protected]4783da3f: startup date [Sun Jan 31 13:12:30 IST 2016]; root of context hierarchy Is DefaultScopedBean singleton? true Is prototypedScoped prototype? true Is prototypedBean prototype? true Is multipleBeansHolder prototype? true Is bean1 prototype? false Is bean1 singleton? true Is bean1AsSingleton singleton? true Is beanA singleton? true Is beanAAsPrototype prototype? true Jan 31, 2016 1:12:30 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose INFO: Closing org.spring[email protected]4783da3f: startup date [Sun Jan 31 13:12:30 IST 2016]; root of context hierarchy
Download the source code
This was an example about spring @Scope annotation.
You can download the source code here: springScopeAnnotationExample.zip