之前有個框架層面的需求是攔截所有的sql進(jìn)行處理馅闽,因為工程里引用了mybatis,jdbc,hibernate,所以直接重寫mysql驅(qū)動的EscapeProcessor類,這時候遇到了第一個問題,EscapeProcessor初始化在spring bean之前。
經(jīng)過一番折騰窄绒,定義了一個spring boot啟動時優(yōu)先加載的配置,很快解決了第一個問題崔兴。
public class ClientBeanProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware {
private ConfigurableListableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException(
"AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory: " + beanFactory);
}
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
// 通過主動調(diào)用beanFactory#getBean來顯示實例化目標(biāo)bean
SpringContextKit springContextKit = this.beanFactory.getBean(SpringContextKit.class);
System.out.println(springContextKit);
}
}
這時坑出現(xiàn)了彰导,明明優(yōu)先初始化的ApplicationContext是注入成功的,但是獲取bean時一直為null敲茄,又經(jīng)過了一番折騰位谋,發(fā)現(xiàn)優(yōu)先加載的bean和獲取的bean不在一個類加載器內(nèi),ClientBeanProcessor 加載SpringContextKit到時用的是RestartClassLoader堰燎,獲取bean使用時用的是AppClassLoader掏父,去掉spring-boot-devtools,問題解決