How to define a bean scope
So, we understand the different scopes and their usage. Now it is time to see how we can use them in coding. We will mainly look at singleton and prototype bean scopes with examples.
Spring provides two different ways to write an application: one is traditional XML metadata configuration, and the second is Java configuration using annotations. Let's look at how XML configuration is used.
XML metadata configuration
In Spring, bean configuration is declared in an XML file of our choice. This file is used by an IoC container to initialize the application context, and at the same time, all bean definitions are initialized based on the provided attribute.
Using the singleton scope
The singleton scope is a very common scope used in major applications. Here, we will start to use the singleton scope. First, we will create a bean class named EmailService
, which consists of a simple getter/setter
method and Constructor
method with a print
statement:
package com.packt.springbean...