@Scopes annotation is used to indicate the scope of a bean, either singleton, prototype, request, session, or a few custom scopes.
To make the EmailService bean class a singleton, we need to annotate the class with @Scope and @Service. So, our EmailService class will look as follows:
package com.packt.springbeanannotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("singleton")
public class EmailService {
private String emailContent;
private String toAddress;
public EmailService() {
System.out.print(" Object of EmailService is Created !!! ");
}
public String getEmailContent() {
return emailContent;
}
public void setEmailContent(String emailContent) {
this.emailContent = emailContent;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
}
We will use the same SpringBeanApplication.java class to test our annotation changes, and the output will also be the same as the XML configuration example.