@Test public void testWithValidEventSubscriber() { final DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("eventBus", BeanDefinitionBuilder.genericBeanDefinition(TestEventBus.class) .getBeanDefinition()); factory.registerBeanDefinition("eventListener", BeanDefinitionBuilder.genericBeanDefinition(ValidEventSubscriber.class).getBeanDefinition()); final SubscribeBeanPostProcessor bpp = new SubscribeBeanPostProcessor(); bpp.setBeanFactory(factory); factory.addBeanPostProcessor(bpp); factory.preInstantiateSingletons(); TestEventBus eventBus = factory.getBean(TestEventBus.class); assertNotNull(eventBus); assertEquals(1, eventBus.subscriptions); factory.destroySingletons(); assertEquals(1, eventBus.unsubscriptions); }
@Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (!(beanFactory instanceof ListableBeanFactory)) { throw new IllegalArgumentException("Expected instance of 'ListableBeanFactory' but got '" + beanFactory.getClass() + "'"); } this.beanFactory = (ListableBeanFactory) beanFactory; }
SubscribeBeanPostProcessor implements DestructionAwareBeanPostProcessor, Ordered, BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (!(beanFactory instanceof ListableBeanFactory)) { throw new IllegalArgumentException("Expected instance of 'ListableBeanFactory' but got '" + beanFactory.getClass() + "'"); } this.beanFactory = (ListableBeanFactory) beanFactory; } }
SubscribeBeanPostProcessor implements DestructionAwareBeanPostProcessor, Ordered, BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (!(beanFactory instanceof ListableBeanFactory)) { throw new IllegalArgumentException("Expected instance of 'ListableBeanFactory' but got '" + beanFactory.getClass() + "'"); } this.beanFactory = (ListableBeanFactory) beanFactory; }  }
SubscribeBeanPostProcessor implements DestructionAwareBeanPostProcessor, Ordered, BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (!(beanFactory instanceof ListableBeanFactory)) { throw new IllegalArgumentException("Expected instance of 'ListableBeanFactory' but got '" + beanFactory.getClass() + "'"); } this.beanFactory = (ListableBeanFactory) beanFactory; }  @Override Object postProcessBeforeInitialization(final Object bean, final String beanName); @Override Object postProcessAfterInitialization(final Object bean, final String beanName); @Override void postProcessBeforeDestruction(final Object bean, final String beanName); @Override int getOrder(); void setOrder(int order); @Override void setBeanFactory(BeanFactory beanFactory); @Override boolean requiresDestruction(Object bean); }
SubscribeBeanPostProcessor implements DestructionAwareBeanPostProcessor, Ordered, BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (!(beanFactory instanceof ListableBeanFactory)) { throw new IllegalArgumentException("Expected instance of 'ListableBeanFactory' but got '" + beanFactory.getClass() + "'"); } this.beanFactory = (ListableBeanFactory) beanFactory; }  @Override Object postProcessBeforeInitialization(final Object bean, final String beanName); @Override Object postProcessAfterInitialization(final Object bean, final String beanName); @Override void postProcessBeforeDestruction(final Object bean, final String beanName); @Override int getOrder(); void setOrder(int order); @Override void setBeanFactory(BeanFactory beanFactory); @Override boolean requiresDestruction(Object bean);  }