让MapperScannerConfigurer程序重新扫描指定的目录,并加载到Spring容器中 public class SpringDynamicLoad {/** * spring-mybatis mapper动态加载 * @param app Spring初始化时创建的容器 * @param basePackage 需要重新
public class SpringDynamicLoad { /** * spring-mybatis mapper动态加载 * @param app Spring初始化时创建的容器 * @param basePackage 需要重新扫描的包 * @author lxycx-xc * 创建时间:2017年9月29日 * */ public static void mybatisMapper(ApplicationContext app,String basePackage){ BeanFactory beanFactory = app.getAutowireCapableBeanFactory(); BeanDefinitionRegistryPostProcessor pp = app.getBean("org.mybatis.spring.mapper.MapperScannerConfigurer#0", BeanDefinitionRegistryPostProcessor.class); MapperScannerConfigurer mapper = (MapperScannerConfigurer) pp; mapper.setApplicationContext(app); mapper.setAddToConfig(true); mapper.setBasePackage(basePackage); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; mapper.postProcessBeanDefinitionRegistry(registry); } }