SpringBoot在开发环境中直接构建DataSource


import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Slf4j
public class StringDevUtil {
    public static DataSource getDevDataSource() throws IOException {
        InputStream ins = StringDevUtil.class.getResourceAsStream("/application-dev.properties");
        Properties prop = new Properties();
        prop.load(ins);
        ins.close();
        String dbClass = prop.getProperty("spring.datasource.driver-class-name");
        String jdbcURL = prop.getProperty("spring.datasource.url");
        String user = prop.getProperty("spring.datasource.username");
        String pass = prop.getProperty("spring.datasource.password");

        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(dbClass);
        ds.setUrl(jdbcURL);
        ds.setUsername(user);
        ds.setPassword(pass);
        return ds;
    }
}

huangyipeng
0 声望2 粉丝

数据感知机


« 上一篇
快速编程之禅