feat: 启动时首选本地文件中数据库的配置进行加载
This commit is contained in:
@@ -3,14 +3,19 @@ package cn.celess.blog;
|
|||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zheng
|
||||||
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableAsync
|
@EnableAsync
|
||||||
@MapperScan("cn.celess.blog.mapper")
|
@MapperScan("cn.celess.blog.mapper")
|
||||||
public class BlogApplication {
|
public class BlogApplication {
|
||||||
|
private static ApplicationContext context;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(BlogApplication.class, args);
|
context = SpringApplication.run(BlogApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,77 @@
|
|||||||
package cn.celess.blog.configuration;
|
package cn.celess.blog.configuration;
|
||||||
|
|
||||||
import com.alibaba.druid.pool.DruidDataSource;
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
* @date : 2019/03/28 14:26
|
* @date : 2019/03/28 14:26
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
public class DruidConfig {
|
public class DruidConfig {
|
||||||
@Value("${spring.datasource.url}")
|
|
||||||
private String dbUrl;
|
|
||||||
|
|
||||||
@Value("${spring.datasource.username}")
|
@Autowired
|
||||||
private String username;
|
Environment env;
|
||||||
|
|
||||||
@Value("${spring.datasource.password}")
|
public static final String DB_CONFIG_PATH = System.getProperty("user.home") + "/blog/application.properties";
|
||||||
private String password;
|
public static final String DB_CONFIG_URL_PREFIX = "spring.datasource.url";
|
||||||
|
public static final String DB_CONFIG_USERNAME_PREFIX = "spring.datasource.username";
|
||||||
@Value("${spring.datasource.driver-class-name}")
|
public static final String DB_CONFIG_PASSWORD_PREFIX = "spring.datasource.password";
|
||||||
private String driverClassName;
|
public static final String DB_CONFIG_DRIVER_CLASS_NAME_PREFIX = "spring.datasource.driver-class-name";
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DruidDataSource druidDataSource() {
|
public DruidDataSource initDataSource() throws IOException {
|
||||||
DruidDataSource dataSource = new DruidDataSource();
|
DruidDataSource dataSource;
|
||||||
dataSource.setDriverClassName(driverClassName);
|
File file = new File(DB_CONFIG_PATH);
|
||||||
// 数据库基本信息
|
if (file.exists()) {
|
||||||
dataSource.setUrl(dbUrl);
|
log.debug("从文件中获取数据库配置");
|
||||||
dataSource.setUsername(username);
|
dataSource = readConfigFromFile(file);
|
||||||
dataSource.setPassword(password);
|
} else {
|
||||||
|
log.debug("默认数据库配置");
|
||||||
// 数据库连接池配置
|
dataSource = defaultDruidSource();
|
||||||
|
}
|
||||||
dataSource.setInitialSize(10);
|
dataSource.setInitialSize(10);
|
||||||
dataSource.setMinIdle(10);
|
dataSource.setMinIdle(10);
|
||||||
dataSource.setMaxActive(100);
|
dataSource.setMaxActive(100);
|
||||||
return dataSource;
|
return dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DruidDataSource readConfigFromFile(File file) throws IOException {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.load(new FileInputStream(file));
|
||||||
|
String url = properties.getProperty(DB_CONFIG_URL_PREFIX, null);
|
||||||
|
String username = properties.getProperty(DB_CONFIG_USERNAME_PREFIX, null);
|
||||||
|
String password = properties.getProperty(DB_CONFIG_PASSWORD_PREFIX, null);
|
||||||
|
String className = properties.getProperty(DB_CONFIG_DRIVER_CLASS_NAME_PREFIX, null);
|
||||||
|
if (url == null || username == null || password == null || className == null) {
|
||||||
|
return defaultDruidSource();
|
||||||
|
}
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
dataSource.setDriverClassName(className);
|
||||||
|
dataSource.setUrl(url);
|
||||||
|
dataSource.setUsername(username);
|
||||||
|
dataSource.setPassword(password);
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private DruidDataSource defaultDruidSource() {
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
dataSource.setDriverClassName(env.getProperty(DruidConfig.DB_CONFIG_DRIVER_CLASS_NAME_PREFIX));
|
||||||
|
dataSource.setUrl(env.getProperty(DruidConfig.DB_CONFIG_URL_PREFIX));
|
||||||
|
dataSource.setUsername(env.getProperty(DruidConfig.DB_CONFIG_USERNAME_PREFIX));
|
||||||
|
dataSource.setPassword(env.getProperty(DruidConfig.DB_CONFIG_PASSWORD_PREFIX));
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
103
src/test/java/cn/celess/blog/configuration/DruidConfigTest.java
Normal file
103
src/test/java/cn/celess/blog/configuration/DruidConfigTest.java
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package cn.celess.blog.configuration;
|
||||||
|
|
||||||
|
import cn.celess.blog.BaseTest;
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class DruidConfigTest extends BaseTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
Environment env;
|
||||||
|
|
||||||
|
private File configFile;
|
||||||
|
private File bakConfigFile;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void initDataSource() throws IOException {
|
||||||
|
|
||||||
|
DruidConfig druidConfig = new DruidConfig();
|
||||||
|
druidConfig.env = env;
|
||||||
|
|
||||||
|
// 无配置文件
|
||||||
|
assertTrue(!configFile.exists() || configFile.delete());
|
||||||
|
DruidDataSource druidDataSource = druidConfig.initDataSource();
|
||||||
|
|
||||||
|
// 加载初始化时候配置文件的数据库连接
|
||||||
|
assertEquals(env.getProperty(DruidConfig.DB_CONFIG_URL_PREFIX), druidDataSource.getUrl());
|
||||||
|
assertEquals(env.getProperty(DruidConfig.DB_CONFIG_DRIVER_CLASS_NAME_PREFIX), druidDataSource.getDriverClassName());
|
||||||
|
assertEquals(env.getProperty(DruidConfig.DB_CONFIG_USERNAME_PREFIX), druidDataSource.getUsername());
|
||||||
|
assertEquals(env.getProperty(DruidConfig.DB_CONFIG_PASSWORD_PREFIX), druidDataSource.getPassword());
|
||||||
|
assertTrue(configFile.createNewFile());
|
||||||
|
|
||||||
|
// 有配置文件的测试
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.load(new FileInputStream(configFile));
|
||||||
|
properties.setProperty(DruidConfig.DB_CONFIG_URL_PREFIX, "jdbc:mysql://localhost:3306/blog");
|
||||||
|
properties.setProperty(DruidConfig.DB_CONFIG_DRIVER_CLASS_NAME_PREFIX, "com.mysql.cj.jdbc.Driver");
|
||||||
|
properties.setProperty(DruidConfig.DB_CONFIG_USERNAME_PREFIX, "username");
|
||||||
|
properties.setProperty(DruidConfig.DB_CONFIG_PASSWORD_PREFIX, "password");
|
||||||
|
// 保存到文件
|
||||||
|
properties.store(new FileOutputStream(configFile), "db config");
|
||||||
|
druidDataSource = druidConfig.initDataSource();
|
||||||
|
assertEquals(properties.getProperty(DruidConfig.DB_CONFIG_URL_PREFIX), druidDataSource.getUrl());
|
||||||
|
assertEquals(properties.getProperty(DruidConfig.DB_CONFIG_DRIVER_CLASS_NAME_PREFIX), druidDataSource.getDriverClassName());
|
||||||
|
assertEquals(properties.getProperty(DruidConfig.DB_CONFIG_USERNAME_PREFIX), druidDataSource.getUsername());
|
||||||
|
assertEquals(properties.getProperty(DruidConfig.DB_CONFIG_PASSWORD_PREFIX), druidDataSource.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void setConfigBack() {
|
||||||
|
if (bakConfigFile.exists()) {
|
||||||
|
System.out.println("恢复配置文件成功");
|
||||||
|
copyFile(bakConfigFile, configFile);
|
||||||
|
bakConfigFile.deleteOnExit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void recordConfig() throws IOException {
|
||||||
|
this.bakConfigFile = new File(DruidConfig.DB_CONFIG_PATH + ".bak");
|
||||||
|
this.configFile = new File(DruidConfig.DB_CONFIG_PATH);
|
||||||
|
if (configFile.exists()) {
|
||||||
|
System.out.println("备份文件成功");
|
||||||
|
copyFile(configFile, bakConfigFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复制文件
|
||||||
|
*
|
||||||
|
* @param src
|
||||||
|
* @param dist
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean copyFile(File src, File dist) {
|
||||||
|
try {
|
||||||
|
// 复制备份文件
|
||||||
|
FileOutputStream fos = new FileOutputStream(dist);
|
||||||
|
FileInputStream fis = new FileInputStream(src);
|
||||||
|
int len = 0;
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
while ((len = fis.read(bytes)) != -1) {
|
||||||
|
fos.write(bytes, 0, len);
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
fis.close();
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user