Loading

Quipoin Menu

Learn • Practice • Grow

spring / Bean Post Processors
interview

Q1. What is a BeanPostProcessor?
BeanPostProcessor is a Spring interface that allows custom processing of bean instances before and after initialization. It has two methods: postProcessBeforeInitialization and postProcessAfterInitialization. These are called for each bean in the container, allowing you to wrap or modify beans.

Q2. Can you give an example of using BeanPostProcessor?
A common use is to create proxies for beans, e.g., for declarative transactions or security. You can implement a custom processor to check for certain annotations and wrap the bean with a proxy. Example:
public class CustomBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) { // do something return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) { // maybe wrap with proxy return bean; } }

Q3. What is the difference between BeanPostProcessor and BeanFactoryPostProcessor?
BeanPostProcessor operates on bean instances after they are created. BeanFactoryPostProcessor operates on bean definitions before any beans are created. It allows you to modify bean definitions, such as property values. Both are executed at different stages of container startup.

Q4. How do you register a BeanPostProcessor?
BeanPostProcessors are automatically detected if you define them as beans in the container (via XML or @Component). Spring will recognize any bean that implements BeanPostProcessor and apply it to all other beans. Order can be controlled by implementing Ordered.

Q5. What are some built-in BeanPostProcessors in Spring?
Examples include AutowiredAnnotationBeanPostProcessor (handles @Autowired), CommonAnnotationBeanPostProcessor (handles @Resource, @PostConstruct), and PersistenceExceptionTranslationPostProcessor (translates JPA exceptions). They enable core Spring features.