Using the prototype scope

As we have seen, the prototype scope is used to get a new instance of a bean every time when requested. To understand prototype, we will take the same bean class, EmailService, and we just need to change the value of the scope attribute for the emailService bean in application-context.xml

<?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.xsd">  <bean id="emailService" class="com.packt.springbean.EmailService"    scope="prototype" />    </beans>

The code used for the singleton scope will the same as before, ...

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.