package org.spreadme.data.mybatis;

import java.util.Set;

import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.mapper.ClassPathMapperScanner;

import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;

public class MybatisMapperScanner extends ClassPathMapperScanner {

    private boolean lazyInitialization;

    public MybatisMapperScanner(BeanDefinitionRegistry registry) {
        super(registry);
        MapperHelper mapperHelper = MapperHelperFactory.getMapperHelper();
        this.setMapperHelper(mapperHelper);
    }

    @Override
    public Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

        for (BeanDefinitionHolder holder : beanDefinitions) {
            GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
            definition.setLazyInit(lazyInitialization);
        }
        return beanDefinitions;
    }

    public void setLazyInitialization(boolean lazyInitialization) {
        this.lazyInitialization = lazyInitialization;
    }
}
