Spring重要組件 介面BeanPostProcessor: 攔截所有容器中的bean,并且可以對bean進行初始化、銷毀操作,
BeanFactoryPostProcessor: 攔截容器,
//攔截整個容器
@Component
public class MyBeanFactory implements BeanFactoryPostProcessor{
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//beanFactory.getBeanDefinition(""); //根據bean的id獲取bean
int count = beanFactory.getBeanDefinitionCount();
System.out.println("容器中bean的個數:"+count);
String[] names = beanFactory.getBeanDefinitionNames();
System.out.println("容器中所有bean的名字:"+ Arrays.asList(names));
}
}
BeanDefinitionRegistryPostProcessor: 即將被加載之前(決議之前,稱為BeanDefination物件之前)
@Component
public class MyListener2 {
//監聽方法
@EventListener(classes = ApplicationEvent.class)
public void myListenerMethod(ApplicationEvent event){
System.out.println("cccccc"+event);
}
}
自定義被監聽事件: 1.自定義類實作ApplicationEvent介面(自定義事件); 2.發布事件,
context.publishEvent(new ApplicationEvent("myEvent") {});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/155852.html
標籤:Java
