The BeanPostProcessor; custom modifications bean instances; use: collecting annotations on custom interface

Interface BeanPostProcessor

  • org.springframework.beans.factory.config.BeanPostProcessor

    • postProcessAfterInitialization(Object bean, String beanName)
    • postProcessBeforeInitialization(Object bean, String beanName)
  • Allowing custom modification example of a new bean plants hooks - for example, with a check mark, or the interface proxy wrapper bean.

  • Typically, the bean filled by marking or the like after the interface processor will implement postprocessbeforeinitialize (java.lang). The use of proxy wrapper bean post-processor is usually achieved postprocessafterinitialize method.

registered

  • ApplicationContext which can automatically detect the BeanPostProcessor bean bean definition, and apply those postprocessor bean any subsequently created. BeanFactory allow for post-processing program to program registration, you apply them to create all the bean by bean factory

Order of priority

  • Automatically detected from the ApplicationContext BeanPostProcessor bean will be sorted and ordered PriorityOrdered semantics. Instead, in order to programmatically apply BeanFactory registered BeanPostProcessor bean will register; will be ignored for the processor programmed manner by any registration or ordering semantics Ordered interface implemented PriorityOrdered expression. Moreover, without taking into account BeanPostProcessor bean of @Order comment.

postProcessAfterInitialization(Object bean, String beanName)

  • Following any bean initialization callback (e.g. InitializingBean of afterPropertiesSet or custom init-method), this BeanPostProcessor applied to a given new bean instance.
  • bean attribute value has been filled. bean instance may return the original bean wrapper.
  • The default implementation returns it as a given bean.

postProcessBeforeInitialization(Object bean, String beanName)

  • Before any bean initialization callback (e.g. InitializingBean of afterPropertiesSet or custom init-method), this BeanPostProcessor applied to a given new bean instance.
  • bean attribute value has been filled. bean instance may return the original bean wrapper.
  • For FactoryBean, this callback will be invoked and the object instance FactoryBean FactoryBean created (from the beginning of Spring 2.0).
  • The processor may be determined after the object is created FactoryBean applied, or applied to both the instanceof FactoryBean checked by respective bean.
  • This callback will also be a method of InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation, rather than all the other BeanPostProcessor callback.
  • The default implementation returns it as a given bean

scenes to be used

  1. Agency services; collect custom annotation on the interface;
    Scene One :收集接口上的自定义注解
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.Set;

/**
 * 收集自定义注解
 *
 * @Author JQ.Wang
 * @Date 2020/4/7
 */

@Repository
@Configuration
@Slf4j
public class CollectCustomAnnotations implements BeanPostProcessor {

    @Getter
    @Setter
    private Set<Object> serviceInstances;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        /** 接口 */
        if (bean.getClass().isAnnotationPresent(CustomAnnotations.class)) {
            if (serviceInstances == null) {
                serviceInstances = new HashSet<>();
            }
            serviceInstances.add(bean);
        }
        return bean;
    }
}
Released eight original articles · won praise 1 · views 138

Guess you like

Origin blog.csdn.net/C_Wangjq/article/details/105371401