Spring @Bean Annotation Example

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();