简单写法:
@Componentpublic class AddMyBeanFactory implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { //实例化bean MemberReaderServiceImpl readerService = new MemberReaderServiceImpl(); //注册单例bean beanFactory.registerSingleton("memberReadService", readerService); }}
用途:在RPC创建动态bean的时候,可以在此处动态创建实例化代理类的方法.可以自定义注解,来使用动态创建代理接口的bean.
@Componentpublic class MyAnnotationAddBeanFactory implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { //1. 扫描指定路径的接口,取得指定需要代理的接口 //2. 根据动态代理,创建bean. //3. 注册bean到 spring bean里面. }}