A prototype scope creates a new instance of a bean in every object initialization. This scope is preferred for the stateful beans. The container doesn't manage the full life cycle of this prototype scope. Here's a code piece for a prototype scope:
<!-- A bean example with prototype scope -->
<bean id = "..." class = "..." scope = "prototype"/>
Let's look at an example of a prototype scope.
Reuse the previous project and modify the bean XML configuration file, as follows:
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="userGreeting" class ="ktPackage.UserGreeting" scope="prototype"/>
</beans>
Once we finish creating the source and bean configuration files, we can run the application. If there's no error, we'll get the following message:
Welcome, Naruto Uzumaki!! <--- value of objectA
Welcome, Sasuke Uchiha!! <--- value of objectB