//攔截整個容器 @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)); } }
@Component public class MyListener2 { //監聽方法 @EventListener(classes = ApplicationEvent.class) public void myListenerMethod(ApplicationEvent event){ System.out.println("cccccc"+event); } }
context.publishEvent(new ApplicationEvent("myEvent") {});