1.代码
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Map; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.origin.OriginTrackedValue; import org.springframework.context.ApplicationListener; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; public class ConfigureListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { Connection conn = null; Statement st = null; ResultSet rs = null; // 获取spring Environment MutablePropertySources propertySources = event.getEnvironment().getPropertySources(); // 配置放在了application-pro或者是application-dev 中 赋值复制需要在其中赋值 for (PropertySource<?> propertySource : propertySources) { boolean applicationConfig = propertySource.getName().contains("application-"); if (!applicationConfig) { continue; } // 获取上文的application集合中获取数据库连接 Map<String, OriginTrackedValue> dataBaseSource = (Map<String, OriginTrackedValue>)propertySource.getSource(); String driverClass = String.valueOf(dataBaseSource.get("spring.datasource.driver-class-name").getValue()); String url = String.valueOf(dataBaseSource.get("spring.datasource.url").getValue()); String user = String.valueOf(dataBaseSource.get("spring.datasource.username").getValue()); String password = String.valueOf(dataBaseSource.get("spring.datasource.password").getValue()); // 因为在spring初始化之前 所有不能使用注解 所以需要jdbc直接连接数据库 首先建立驱动 try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url, user, password); // 1、获取连接对象 // 2、创建statement类对象,用来执行SQL语句!! st = conn.createStatement(); // 3、创建sql查询语句 String sql = "select * from SYS_CONFIGURE"; // 4、执行sql语句并且换回一个查询的结果集 rs = st.executeQuery(sql); while (rs.next()) { // 获取数据库中的数据 String item = rs.getString("ITEM"); String itemValue = rs.getString("ITEM_VALUE"); // 通过数据库中的配置 修改application集合中数据 Map<String, OriginTrackedValue> source = (Map<String, OriginTrackedValue>)propertySource.getSource(); OriginTrackedValue originTrackedValue = source.get(item); OriginTrackedValue newOriginTrackedValue = OriginTrackedValue.of(itemValue, originTrackedValue.getOrigin()); source.put(item, newOriginTrackedValue); } } catch (Exception e) { e.printStackTrace(); } } } }
@SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(TestApplication.class); app.addListeners(new ConfigureListener()); app.run(args); } }
2.简介
在spring boot初始化时 会读取application 放在MutablePropertySources 中 我们会读取配置MutablePropertySources 然后查询数据库 根据key查询之后 重新覆盖(因为以上操作时 bean factory还没有初始化 所以用jdbc)
而以上拦截器会在读取application后立刻执行 不会马后炮(当spring boot初始化之后在更改MutablePropertySources )
获取MutablePropertySources MutablePropertySources propertySources = event.getEnvironment().getPropertySources();