In this post under Spring Core, I will show with example how to order multiple “BeanPostProcessor” interface implementations.
To order multiple “BeanPostProcessor” implementations, each implementations much implement “Ordered” interface and provide order number in the interface “getOrder” method.
Note: Ordering of “BeanPostProcessor” implementations only work with “@Component” annotation and not with “@Bean” annotation.
For our example, I will use the below “BeanPostProcessor” interface implementations
LogbackBeanPostProcessor
package core.package55;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.core.Ordered;import org.springframework.core.PriorityOrdered;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Componentpublic class LogbackBeanPostProcessor implements BeanPostProcessor, Ordered { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Logging by Logback before initialization of bean: " + beanName); return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName); } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Logging by Logback after initialization of bean: " + beanName); return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); } @Override public int getOrder() { return 1; }}
Log4jBeanPostProcessor
package core.package55;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.core.Ordered;import org.springframework.core.PriorityOrdered;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Componentpublic class Log4jBeanPostProcessor implements BeanPostProcessor, Ordered { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Logging by Log4j before initialization of bean: " + beanName); return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName); } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Logging by Log4j after initialization of bean: " + beanName); return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); } @Override public int getOrder() { return 2; }}
As you can see from the above code, both “BeanPostProcessor” implementations “Log4jBeanPostProcessor” and “LogbackBeanPostProcessor” implements “Ordered” interface and provide order number in “getOrder”
method. “Log4jBeanPostProcessor” return 2 in “getOrder” method, whereas “LogbackBeanPostProcessor” return 1 in “getOrder” method.
As a result, “LogbackBeanPostProcessor” is called first before “Log4jBeanPostProcessor” class.
Below are the classes to which “BeanPostProcessor” implementations are to be applied.
FileManager
package core.package55;import org.springframework.stereotype.Component;@Componentpublic class FileManager { public void upload(String fileName) { System.out.println("Uploading file with name: " + fileName); }}
Main class
package core.package55;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan(basePackages = "core.package55")public class Example55 { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example55.class); }}
Below is the output
Output
Logging by Logback before initialization of bean: example55Logging by Log4j before initialization of bean: example55Logging by Logback after initialization of bean: example55Logging by Log4j after initialization of bean: example55Logging by Logback before initialization of bean: fileManagerLogging by Log4j before initialization of bean: fileManagerLogging by Logback after initialization of bean: fileManagerLogging by Log4j after initialization of bean: fileManager
In this way we can order multiple “BeanPostProcessor” implementations.