Posts

Showing posts with the label Spring

Spring @Bean Annotation Example

Image
In this section we will learn about @Bean  Annotation. Spring  @Bean  Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework. Create MessageService interface for message service implementations public interface MessageService { String getMessage (); void setMessage ( String message); } Create implementation - SMSService class.  public class SMSService implements MessageService { private String message ; @Override public String getMessage () { return message ; } @Override public void setMessage ( String message) { this . message = message; } } Here is a Configuration class where we have defined a @Bean method for SMSService class. @Configuration public class AppConfig { @Bean public MessageService smsService () { return new SMSService();

Spring @Scope Annotation Example

Image
In this section we will learn about @Scope  Annotation The scope of a bean defines the life cycle and visibility of that bean in the contexts in which it is used.  A bean’s scope is set using the @Scope annotation.  Singleton scope is the default scope in spring. Means, the Spring framework creates exactly one instance for each bean declared in the IoC container . The scopes supported out of the box are listed below: The last three scopes mentioned request , session , and globalSession are only available in a web-aware application. We use @Scope to define the scope of a @Component , @Service , and @Repository class or a @Bean definition. 1. Use  @Scope  to define the scope of a  @Service. @Service @Scope (value = ConfigurableBeanFactory . SCOPE_PROTOTYPE ) //@Scope(value="prototype") public class EmailService implements MessageService { private String message ; @Override public String getMessage () { return message ; } @Override public v