Posts

Showing posts with the label @Bean

Spring Core | BeanFactoryPostProcessor | Example

Image
In Spring, the  BeanFactoryPostProcessor is a  functional interface that contains a abstract method postProcessBeanFactory .   It allows us to modify the Spring context’s bean definitions before any beans get created.  BeanFactoryPostProcessor can create new bean definitions or modify existing ones. Since  BeanFactoryPostProcessor  should be called before other bean types are formed, it must be registered as a static method level. BeanFactoryPostProcessor example We are creating a simple maven project. You could clone the code from our GitHub repo. Final Project Directory Complete pom.xml <? xml version ="1.0" encoding ="UTF-8" ?> < project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < gr

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