All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.tenfell.plugins.dbgenerate.component.DbGenerateComponent Maven / Gradle / Ivy

package cn.tenfell.plugins.dbgenerate.component;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.tenfell.plugins.dbgenerate.config.DbGenerateProperties;
import cn.tenfell.plugins.dbgenerate.utils.DocUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashSet;
import java.util.Set;

@Component
public class DbGenerateComponent implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DbGenerateProperties properties = applicationContext.getBean(DbGenerateProperties.class);
        String password = properties.getPassword();
        Set beanPackages = properties.getBeanPackages();
        if(beanPackages != null && beanPackages.size()>0){
            if(DocUtils.dbGenerateProperties.getBeanPackages() == null){
                DocUtils.dbGenerateProperties.setBeanPackages(new HashSet());
            }
            DocUtils.dbGenerateProperties.getBeanPackages().addAll(beanPackages);
        }
        if(StrUtil.isBlank(DocUtils.dbGenerateProperties.getPassword()) && StrUtil.isNotBlank(password)){
            DocUtils.dbGenerateProperties.setPassword(password);
        }
        String[] names = applicationContext.getBeanNamesForType(DataSource.class);
        DataSource dataSource = null;
        for(String name:names){
            Object temp = applicationContext.getBean(name, DataSource.class);
            dataSource = (DataSource)temp;
            break;
        }
        if(dataSource != null){
            DocUtils.init(dataSource);
        }else{
            Assert.isTrue(false,"找不到当前数据源");
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy