Spring boot 擴展

介紹spring的三個類java

   BeanPostProccorspring

   BeanFactoryProccorpost

   BeanDefinitionRegistryProcessorui

 

  @ BeanPostproccor 會初始化的以前,以及初始化以後執行spa

  @ BeanFactoryPostProccor 只執行一次,會在BeanPostProccor  before方法執行的前面code

  @BeanDefinitionRegistryProcessor 動態的建立benablog

@Component
public class EsBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        for(int i = 0 ;i < 10; i++){
            BeanDefinitionBuilder builder=BeanDefinitionBuilder.rootBeanDefinition(User.class);
            builder.addPropertyValue("name","name="+i);
            beanDefinitionRegistry.registerBeanDefinition("user"+i,builder.getBeanDefinition());
        }
    }
}
public class App {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext("com.example.demo.day3");
        context.getBeansOfType(User.class).values().forEach(System.out::println);
        context.close();
    }
}
相關文章
相關標籤/搜索