When you declare a POJO instance or bean in the configuration file, you are actually defining a template for bean creation, not an actual bean instance. The actual bean instance is created when the getBean() method is called. While bean is instantiated by the Spring IoC, the framework decides the scope of the instance.

Following are the different bean instance scopes supported in Spring.

  • singleton – Creates at most one bean instance per Spring IoC container
  • prototype – Creates a new instance each requested
  • request – Creates a single bean instance per HTTP request;
  • session – Creates a single bean instance per HTTP session;
  • global session- Creates a single bean instance per global HTTP session

You can set the bean scope using the <bean> element scope attribute. The default scope fro bean is set to Singleton. This means that only one instance of bean can be created in the IoC container and the same instance can be shared across.

The following code snippet shows how to define the bean scope using XML configuration.

<bean id="crayon" class="com.javatechig.spring.bean.Crayon" scope="prototype">
      <property name="name" value="Dancing Crayon" />
      <property name="price" value="30" />

You can also specifying bean scope from Java code using @Scope annotation as follows:

public class MyBeanConfig {
	@Scope(value = BeanDefinition.SCOPE_SINGLETON)
	public Crayon crayonToy() {
		return new Crayon("Yellow crayon", 50);

This article is about: Spring Core,


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.