View Discussion Show Improve Article Save Article Bean Scopes refers to the lifecycle of Bean that means when the object of Bean will be instantiated, how long does that object live, and how many objects will be created for that bean throughout. Basically, it controls the instance creation of the bean and it is managed by the spring container.
Let’s us see some of them in detail: Singleton Scope:If the scope is a
singleton, then only one instance of that bean will be instantiated per Spring IoC container and the same instance will be shared for each request. That is when the scope of a bean is declared singleton, then whenever a new request is made for that bean, spring IOC container first checks whether an instance of that bean is already created or not. If it is already created, then the IOC container returns the same instance otherwise it creates a new instance of that bean only at the first request.
By default, the scope of a bean is a singleton.
Javapackage bean; public class HelloWorld { public String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
XML
Javapackage driver; import org.springframework .context.ApplicationContext; import org.springframework .context.support .ClassPathXmlApplicationContext; import bean.HelloWorld; public class Client { public static void main(String[] args) { ApplicationContext ap = new ClassPathXmlApplicationContext( "resources/spring.xml"); HelloWorld Geeks1 = (HelloWorld)ap.getBean("hw"); Geeks1.setName("Geeks1"); System.out.println( "Hello object (hello1)" + " Your name is: " + Geeks1.getName()); HelloWorld Geeks2 = (HelloWorld)ap.getBean("hw"); System.out.println( "Hello object (hello2)" + " Your name is: " + Geeks2.getName()); System.out.println( "'Geeks1' and 'Geeks2'" + " are referring" + "to the same object: " + (Geeks1 == Geeks2)); System.out.println( "Address of object Geeks1: " + Geeks1); System.out.println( "Address of object Geeks2: " + Geeks2); } }
Prototype Scope:If the scope is declared prototype, then spring IOC container will create a new instance of that bean every time a request is made for that specific bean. A request can be made to the bean instance either
programmatically using getBean() method or by XML for Dependency Injection of secondary type. Generally, we use the prototype scope for all beans that are stateful, while the singleton scope is used for the stateless beans.
Java
package bean; public class HelloWorld { public String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
XML
Javapackage driver; import org.springframework .context.ApplicationContext; import org.springframework.context.support .ClassPathXmlApplicationContext; import bean.HelloWorld; public class Client { public static void main(String[] args) { ApplicationContext ap = new ClassPathXmlApplicationContext( "resources/spring.xml"); HelloWorld Geeks1 = (HelloWorld)ap.getBean("hw"); Geeks1.setName("Geeks1"); System.out.println( "Hello object (hello1)" + " Your name is: " + Geeks1.getName()); HelloWorld Geeks2 = (HelloWorld)ap.getBean("hw"); System.out.println( "Hello object (hello2)" + "Your name is: " + Geeks2.getName()); System.out.println( "'Geeks1' and 'Geeks2'" + "are referring " + "to the same object: " + (Geeks1 == Geeks2)); System.out.println( "Address of object Geeks1: " + Geeks1); System.out.println( "Address of object Geeks2: " + Geeks2); } }
Is it possible to make JVM scoped bean to be available for the Spring container?Single pattern in java mean you can create the only one instance of a that class in JVM. But In spring singleton bean scope means every container can create only single bean in the Spring IoC Container but a JVM can have multiple Spring IoC Container so JVM can multiple beans rather than bean singleton bean scope.
What is the default scope of a bean in Spring container?singleton scope. singleton is default bean scope in spring container. It tells the container to create and manage only one instance of bean class, per container. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached instance.
How do you define a prototype scope for a bean in a Spring boot?When a spring bean is scoped as a prototype, the Spring IoC container creates new bean instance every time when a request is made for that bean. We can define the scope of a bean as prototype using scope="prototype" attribute of element or using @Scope(value = ConfigurableBeanFactory. SCOPE_PROTOTYPE) annotation.
How can use session scoped bean in Spring?The @Scope annotation can define a spring bean's scope. It has two arguments that are value and proxyMode.. To define a request scope spring bean, set the value argument to WebApplicationContext. ... . To define a session scope spring bean, set the value argument to WebApplicationContext.. |