package com.huashe.park.application.mybatis;

import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class RegisterCustomerInterceptor implements ApplicationListener<ContextRefreshedEvent> {
    /**
     * 需要注册的自定义插件
     */
    @Autowired
    private MybatisInterceptor mybatisInterceptor;

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactories;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        for (SqlSessionFactory factory : sqlSessionFactories) {
            factory.getConfiguration().setObjectWrapperFactory(new MapWrapperFactory());
            // 由于mybatis拦截器使用责任链模式,有可能会导致自定义拦截器失效,因此下面方法可以将自定义拦截器重新注入到sql中
            factory.getConfiguration().addInterceptor(mybatisInterceptor);
        }
    }
}
