Posts

Showing posts with the label Spring Core

Spring Core - BeanPostProcessor - Example

Image
In Spring framework, the BeanPostProcessor is an interface that contains two callback methods: postProcessBeforeInitialization  and postProcessAfterInitialization .  BeanPostProcessor allows custom modification of new bean instances that are created by Spring Bean Factory.  If we want to implement some custom logic such as checking for marker interfaces or wrapping beans with proxies after the Spring container finishes instantiating, configuring, and initializing a bean by plugging in one or more BeanPostProcessor implementations. As we mentioned earlier, BeanPostProcessor consists of two callback methods: 1.  postProcessBeforeInitialization :  We can call this method to apply any custom logic (such as InitializingBean's afterPropertiesSet or a custom init-method) to the given new bean instance before any bean initialization callbacks. 2.  postProcessAfterInitialization :  We can call this method to apply any custom logic (such as InitializingBean's afterPropertiesSet or a cust

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 initMethod and destroyMethod Example

Image
The initMethod and destroyMethod  are attributes of Spring @Bean annotation to perform certain actions upon bean initialization and destruction.  initMethod  is called after bean initialization and the  destroyMethod  is called before bean destruction by container. Annotations  @PostConstruct and @PreDestroy , standardized by JSR-250, are generally considered the best practice for obtaining lifecycle callbacks in a modern Spring application.   initMethod and destroyMethod are alternatives to InitializingBean and DisposableBean . Related topic, Spring init-method and destroy-method - click here Complete 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://