com.easyshop.datasource.MyBatisBeanFactoryPostProcessor.java Source code

Java tutorial

Introduction

Here is the source code for com.easyshop.datasource.MyBatisBeanFactoryPostProcessor.java

Source

/*
 * @(#)MyBatisAdaperBeanFactoryPostProcessor.java $version 2014. 3. 7.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.easyshop.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.core.Ordered;

/**
 * MyBatis? SqlSessionFactory ?? mapper xml  .
 * @author mentalese
 */
public class MyBatisBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {
    private final Logger log = LoggerFactory.getLogger(MyBatisBeanFactoryPostProcessor.class);

    private String sqlSessionFactoryBeanName;
    private String[] mapperLocations;

    /**
     * @param beanFactory
     * @throws BeansException
     * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
     */
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        BeanDefinition bd = beanFactory.getBeanDefinition(sqlSessionFactoryBeanName);
        MutablePropertyValues propertyValues = bd.getPropertyValues();
        PropertyValue propertyValue = propertyValues.getPropertyValue("mapperLocations");
        Object value = propertyValue.getValue();

        ManagedList<TypedStringValue> locations = new ManagedList<TypedStringValue>();
        for (String location : mapperLocations) {
            locations.add(new TypedStringValue(location));
            log.info(" SQL Mapper  :{} " + location);
        }

        if (value == null) {
            PropertyValue newValue = new PropertyValue(propertyValue, locations);
            propertyValues.addPropertyValue(newValue);
        } else if (value instanceof String) {
            locations.add(new TypedStringValue((String) value));
            PropertyValue newValue = new PropertyValue(propertyValue, locations);
            propertyValues.addPropertyValue(newValue);
        } else if (value instanceof ManagedList) {
            ((ManagedList) value).addAll(locations);
        } else if (value instanceof TypedStringValue) {
            locations.add((TypedStringValue) value);
            PropertyValue newValue = new PropertyValue(propertyValue, locations);
            propertyValues.addPropertyValue(newValue);
        }
    }

    public String getSqlSessionFactoryBeanName() {
        return sqlSessionFactoryBeanName;
    }

    public void setSqlSessionFactoryBeanName(String sqlSessionFactoryBeanName) {
        this.sqlSessionFactoryBeanName = sqlSessionFactoryBeanName;
    }

    public String[] getMapperLocations() {
        return mapperLocations;
    }

    public void setMapperLocations(String[] mapperLocations) {
        this.mapperLocations = mapperLocations;
    }

    @Override
    public int getOrder() {
        return 1000;
    }

}