创建配置注入测试类
@Configuration
@ConfigurationProperties(prefix = ConfigTestPre.PREFIX)
@Data
public class ConfigTestPre {
public static final String PREFIX = "hx.config";
@Value("${hx.config:}") // 会优先使用prefix的方式注入
private String test = "1";
private String cfg;
}
1、自定义配置文件的注入
public class CustomerConfigInject implements EnvironmentPostProcessor {
PropertiesPropertySourceLoader load=new PropertiesPropertySourceLoader();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
MutablePropertySources propertySources = environment.getPropertySources();
Resource resource=new ClassPathResource("custom.properties");
PropertySource propertySource;
try {
propertySource = load.load("customProperties",resource).get(0);
propertySources.addFirst(propertySource);
} catch (IOException e){
e.printStackTrace();
}
}
}
META-INF/spring.factories中指定配置类:
org.springframework.boot.env.EnvironmentPostProcessor=com.hx.temp.config.CustomerConfigInject
在resource文件夹中添加自己的配置文件 custom.properties
hx.config=123
2、从数据库查询配置进行注入
数据库sql
DROP TABLE IF EXISTS `setting_table`;
CREATE TABLE `setting_table` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ' ',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `whx`.`setting_table`(`id`, `name`, `value`) VALUES (1, 'hx.config.test', 'fromMysql-test');
INSERT INTO `whx`.`setting_table`(`id`, `name`, `value`) VALUES (2, 'hx.config.cfg', 'fromMysql-cfg');
查询数据库配置,手动绑定配置到指定配置类中
项目启动执行一次,后面可以调接口刷新配置
package com.hx.config.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.*;
/**
* @ClassName ConfigSupportConfiguration
* @Description //TODO
* @Author WHX
* @Date 2022/6/5 15:00
**/
@Component
@Slf4j
public class ConfigSupportConfiguration {
public static final String SQL = "select id, name, value from setting_table";
private static final String SOURCE_NAME = ConfigSupportConfiguration.class.getSimpleName();
@Value("${hx.config.load_sql:" + SQL + "}")
private final String settings_sql = SQL;
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private DataSource dataSource;
@Autowired
private ConfigTestPre configTestPre;
@PostConstruct
public Object refreshMysqlInjectConfig() {
// 从数据库查询配置
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
final List<Map<String, Object>> rows = jdbcTemplate.queryForList(settings_sql);
// 获取或者创建 PropertySource
MapPropertySource propertySource = Optional.ofNullable((MapPropertySource) environment.getPropertySources().get(SOURCE_NAME))
.orElse(new MapPropertySource(SOURCE_NAME, new HashMap<>()));
Map<String, Object> source = propertySource.getSource();
Map<String, Object> updateSource = new HashMap<>();
for (final Map<String, Object> row : rows) {
String name = String.valueOf(row.get("name"));
String value = String.valueOf(row.get("value"));
if (!StringUtils.hasText(name) || value == null) {
continue;
}
Object property = source.get(name);
if (null != property && property.equals(value)) {
// 配置未发生改变
continue;
}
source.put(name, value);
updateSource.put(name, value);
}
if (updateSource.isEmpty()) return "{}";
environment.getPropertySources().addLast(propertySource);
// 配置注入早已经执行过了,所以这里手动去执行配置绑定到指定类(prefix的方式绑定)
// 注意:这个仅针对configTestPre一个类进行了手动配置注入,并不是刷新所有的配置类;
Binder.get(environment).bind(configTestPre.PREFIX, Bindable.ofInstance(configTestPre));
return updateSource;
}
}
刷新配置
定义刷新配置的接口
@RestController
@RequestMapping("/config/refresh")
public class RefreshConfigController {
@Autowired
private ConfigSupportConfiguration configSupportConfiguration;
@GetMapping("/mysqlInject")
public Object refreshMysqlInjectConfig(){
return configSupportConfiguration.refreshMysqlInjectConfig();
}
}
修改数据库后,调用刷新接口:
curl localhost:80/config/refresh/mysqlInject