Singleton scope with annotation

@Scopes annotation is used to indicate the scope of a bean, either singleton, prototyperequest, session, or a few custom scopes.

To make the EmailService bean class a singleton, we need to annotate the class with @Scope and @ServiceSo, 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79