001package com.unitils.boot.autoconfigure;
002
003import org.springframework.beans.BeansException;
004import org.springframework.beans.factory.config.BeanPostProcessor;
005import org.springframework.stereotype.Component;
006import org.unitils.database.UnitilsDataSourceFactoryBean;
007
008@Component
009public class DataSourcePostProcessor implements BeanPostProcessor {
010
011    @Override
012    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
013        return bean;
014    }
015
016    @Override
017    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
018        if (beanName.equals("dataSource")) {
019            try {
020                return new UnitilsDataSourceFactoryBean().getObject();
021            } catch (Exception exp) {
022                throw new RuntimeException("replace database throw exception ,can not continue to process", exp);
023            }
024        }
025        return bean;
026    }
027}