1、BeanFactoryPostProcessor
新建自定义的bean工厂的后置处理器MyBeanFactoryPostProcessor,交给Spring管理
MyBeanFactoryPostProcessor.java
package springextends;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// obtain the the beanFactory, you can do something that beanFactory allowed
Person person = (Person) beanFactory.getBean("person");
System.out.println("====before======="+ person.getName());
person.setName("kebo");
System.out.println("=====after======"+ person.getName());
}
}
2、BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口
MyBeanDefinitionRegisterPostProcessor.java
package springextends;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;
@Component
public class MyBeanDefinitionRegisterPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("before postProcessBeanDefinitionRegistry===All bean definition count is " + registry.getBeanDefinitionCount());
registry.registerBeanDefinition("car", new RootBeanDefinition(Car.class));
System.out.println("after postProcessBeanDefinitionRegistry===All bean definition count is " + registry.getBeanDefinitionCount());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// do someting you want
}
}
3、ApplicationLister
自定义事件监听器,ApplicationListener想监听什么事件就传入什么类型的ApplicationEvent
MyApplicationLister.java
package springextends_applicationListener;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationLister implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("received event is " + event);
}
}