@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=1232、从数据库查询配置进行注入 数据库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.*;
@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
刷新配置
定义刷新配置的接口
@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



