栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot自定义配置注入的方式:自定义配置文件注入,从mysql读取配置进行注入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot自定义配置注入的方式:自定义配置文件注入,从mysql读取配置进行注入

创建配置注入测试类
@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.*;


@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> rows = jdbcTemplate.queryForList(settings_sql);

        // 获取或者创建 PropertySource
        MapPropertySource propertySource = Optional.ofNullable((MapPropertySource) environment.getPropertySources().get(SOURCE_NAME))
                .orElse(new MapPropertySource(SOURCE_NAME, new HashMap<>()));
        
        Map source = propertySource.getSource();
        Map updateSource = new HashMap<>();

        for (final Map 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952622.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号