SpringBoot 数据源灵活修改 读取外部配置文件

① 控制器

 

import com.wskj.project.dataSourceConfig.DataSourceConfig;
import com.wskj.project.model.ResponseResult;
import com.wskj.project.service.impl.DBServiceImpl;
import com.wskj.project.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api("数据源设置Controller")
public class DataSourceController {
    private Logger logger = LoggerFactory.getLogger(DataSourceController.class);
    @Resource
    private DBServiceImpl dbService;
    public static int countDB=0;
    @Resource
    private DataSourceConfig dataSourceConfig;

    //测试数据库连接
    private static  Connection con;//声明Connection对象
    private static   Statement sql;
    private static  ResultSet res;

    @ApiOperation(value = "设置数据源", notes = "返回信息 0成功,400失败 ")
    @RequestMapping(value = "/setDB", method = RequestMethod.POST)
    public ResponseResult setDB(@ApiParam(required =true, name = "driverClassName", value = "数据库驱动")String driverClassName,
                                @ApiParam(required =true, name = "url", value = "连接地址") String url,
                                @ApiParam(required =true, name = "username", value = "用户名")String username,
                                @ApiParam(required =true, name = "password", value = "密码") String password) {//前端非空判断了
        if(driverClassName!=null&&!"".equals(driverClassName)&&url!=null&&!"".equals(url)&&username!=null&&!"".equals(username)&&password!=null&&!"".equals(password)){
            if(countDB>=8){
                return new ResponseResult(ResponseResult.OK, "密码错误"+countDB+"次,无法设置数据源,请确认密码及用户名,", false);
            }
            boolean isOk=dbService.getUser(username.toUpperCase());
            if(isOk){
                Connection  conss=getTestDB(driverClassName,url,username,password);
                if(conss!=null){
                    try {
                        PropertiesConfiguration properties = new PropertiesConfiguration( StringUtil.getRealPathByPack());
                        properties.setProperty("spring.datasource.driverClassName", driverClassName);
                        properties.setProperty("spring.datasource.url", url);
                        properties.setProperty("spring.datasource.username", username);
                        properties.setProperty("spring.datasource.password", password);
                        properties.save();
                        dataSourceConfig.changeDataSource();//重新启动
                        countDB=0;
                        return new ResponseResult(ResponseResult.OK, "设置数据源成功", true);
                    } catch (Exception e) {
                        logger.error("设置数据源失败:" + e);
                        countDB++;
                        return new ResponseResult(ResponseResult.OK, "设置数据源失败,请检查填写信息是否正确,异常信息:" + e.getMessage(), false);
                    }
                }else{
                    countDB++;
                    return new ResponseResult(ResponseResult.OK, "密码错误"+countDB+"次,设置数据源失败,请检查密码是否正确", false);
                }

            }else {
                return new ResponseResult(ResponseResult.OK, "设置数据源失败,请检查用户名是否正确", false);
            }
        }else{
            return new ResponseResult(ResponseResult.OK, "设置数据源失败,请检查输入框不能为空", false);
        }
    }

    @ApiOperation(value = "获取数据源", notes = "返回信息 0成功,400失败 ")
    @RequestMapping(value = "/getDB", method = RequestMethod.GET)
    public ResponseResult getDB() {
        Map db = new HashMap<>();
        try {
            PropertiesConfiguration properties = new PropertiesConfiguration( StringUtil.getRealPathByPack());
            db.put("driverClassName", properties.getString("spring.datasource.driverClassName"));
            db.put("url", properties.getString("spring.datasource.url"));
            db.put("username", properties.getString("spring.datasource.username"));
            db.put("password", properties.getString("spring.datasource.password"));
        } catch (Exception e) {
            logger.error("获取数据库配置文件失败:" + e);
            return new ResponseResult(ResponseResult.OK, e.getMessage(), false);
        }
        countDB=0;
        return new ResponseResult(ResponseResult.OK, "获取数据库配置成功", db, true);
    }

    /**
     * 测试数据库连接
     * @return
     */
    private Connection getTestDB(String driverClassName, String url, String username, String password){
        try {
            if(countDB==9){
                return null;
            }
            //加载数据库驱动类
            Class.forName(driverClassName);
//            System.out.println("数据库驱动加载成功");  //返回加载驱动成功信息
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        try {
            con= DriverManager.getConnection(url,username,password);//通过访问数据库的URL获取数据库连接对象 ,这里后两个参数分别是数据库的用户名及密码
//            System.out.println("数据库连接成功");  //返回连接成功信息
        }catch(SQLException e) {
            con=null;
//            System.out.println("数据库连接失败");
        }
        return con;//按方法要求返回一个Connection对象
    }

}

 

DataSourceConfig 配置文件

import com.alibaba.druid.pool.DruidDataSource;
import com.wsda.project.util.StringUtil;
import com.wsda.wsda.WsdaApplication;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Description:
 * @date 2019
 */
@Configuration
public class DataSourceConfig {
    private static Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
    @Resource
    private DataSourceConfig dbConfig;
    @Bean(name = "dataSource")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        try {
            PropertiesConfiguration properties = new PropertiesConfiguration( StringUtil.getRealPathByPack());
            String driverClassName = properties.getString("spring.datasource.driverClassName");
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(properties.getString("spring.datasource.url"));
            dataSource.setUsername(properties.getString("spring.datasource.username"));
            dataSource.setPassword(properties.getString("spring.datasource.password"));
        } catch (Exception e) {
            logger.error("获取数据库配置文件失败" + e);
        }
        return dataSource;
    }
 
    /**
     * 动态修改数据库链接
     */
    public void changeDataSource () {
        Thread restartThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    WsdaApplication.restart();
                } catch (Exception ignored) {
                    System.out.println("启动重启项目"+ignored.getMessage());
                }
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
    }
}

③启动类修改

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@MapperScan(basePackages="com.wsda.project.dao")
@ComponentScan("com.wsda.project.*")
@EnableSwagger2
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)//添加事务
public class WsdaApplication {

    private static String[] args;
    private static ConfigurableApplicationContext context;


    public static void main(String[] args) {
        WsdaApplication.args = args;
        WsdaApplication.context = SpringApplication.run(WsdaApplication.class, args);
    }
    public static void restart() {
        context.close();//先关闭上下文 然后重新启动项目
        WsdaApplication.context =SpringApplication.run(WsdaApplication.class, args);

    }
}

 

④StringUtil 工具类的获取外置配置文件的方法 随意能获取到就行

/***
 * 打包获取项目配置文件路径
 * @return
 */
public static String getRealPathByPack() {
    String realPath = StringUtil.class.getClassLoader().getResource("").getFile();
    File file = new File(realPath);
    realPath = file.getAbsolutePath();//去掉了最前面的斜杠/
    try {
        realPath=realPath.substring(realPath.lastIndexOf(":")-1,realPath.indexOf("FormDesigner"))+"config\\db.properties";//配置文件跟打包的项目放在同级目录下
        System.out.println(realPath);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return realPath;
}

配置db.properties

SpringBoot 数据源灵活修改 读取外部配置文件_第1张图片

前端页面

SpringBoot 数据源灵活修改 读取外部配置文件_第2张图片

目的是为了可以随时切换数据库,不用手动重启项目打包发布后修改也方便


 

你可能感兴趣的:(java,SpringBoot)