2 Star 0 Fork 0

springboot-source/自定义environmentPostProcessor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

自定义environmentPostProcessor

介绍

基于mysql实现mini版的分布式配置文件组件操作。

代码实现

  • 创建分布式配置表及插入测试值

    CREATE TABLE `dis_config` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '分布式key值',
      `value` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '分布式值',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ;
    
    INSERT INTO `dis_config` VALUES (1, 'user.name', 'helloworld');
    INSERT INTO `dis_config` VALUES (2, 'user.province', '江苏');
    
  • 简单实现一个数据库连接操作

    public class JdbcUtil {
    
        public static Connection getConnection() {
            Connection connection = null;
            try {
    //            connection = DriverManager.getConnection("jdbc:mysql://172.17.125.116:3306/bsxmdb?useUnicode=true&characterEncoding=utf8&useSSL=false", "bsxmdb", "GPRKtx8NjjEY");
                connection = DriverManager.getConnection("jdbc:mysql://1902.168.31.218:3306/config?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "lovety");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }
    
    
  • 定义基础的属性对象DBProperty

    public class DBProperty {
    
        //最终的读取数据库配置走的是这里
        public String getValue(String name) {
            String value = null;
            Connection connection = JdbcUtil.getConnection();
            try {
                PreparedStatement preparedStatement = connection.prepareStatement("select value from dis_config where name = ?");
                preparedStatement.setString(1,name);
                ResultSet resultSet = preparedStatement.executeQuery();
                while (resultSet.next()){
                    value =   resultSet.getString(1);
                }
                System.out.println("你查询的分布式key="+name+" ,value="+value);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                if (connection != null){
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
            return value;
        }
    }
    
    
  • 定义DBPropertySource对象

    
    // DBPropertySource 继承基础的PropertySource对象
    public class DBPropertySource  extends PropertySource<DBProperty> {
    
        private DBProperty dbProperty;
    
    
        public DBPropertySource(String name,DBProperty dbProperty){
            super(name,dbProperty);
            this.dbProperty = dbProperty;
        }
    
        @Override
        public Object getProperty(String name) {
            return dbProperty.getValue(name);
        }
    }
    
    
  • 定义EnvironmentPostProcessor这个也是最重要,这里赋值一个新的PorpertySource给Environment

    public class HanggoldEnvironmentPostProcessor implements EnvironmentPostProcessor {
    
        private static final String OWN_DIS_CONFIG_NAME="ownDisConfigSource";
    
    
        @Override
        public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    
            DBProperty dbProperty = new DBProperty();
    
            DBPropertySource dbPropertySource = new DBPropertySource(OWN_DIS_CONFIG_NAME,dbProperty);
    
            environment.getPropertySources().addLast(dbPropertySource);
    
        }
    }
    
    • 想让 HanggoldEnvironmentPostProcessor被springboot启动时运行需要增加springboot的自动装备的原理。在classpath中新建目录 META-INF 然后创建spring.factories文件,在自定义的spring.factories中配置如下。

      org.springframework.boot.env.EnvironmentPostProcessor=\
      com.hanggold.springboot.environment.post.HanggoldEnvironmentPostProcessor
      
      
  • 测试controller

    @RestController
    @RequestMapping("config")
    public class ConfigController {
    
        @Value("${user.name}")
        private String username;
    
        @Value("${user.province}")
        private String userProvince;
    
        @GetMapping("username")
        public String configUsername(){
            return username;
        }
    
        @GetMapping("userprovince")
        public String configUserProvince(){
            return username;
        }
    }
    

空文件

简介

自己实现mini版的分布式配置文件 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/springboot-source/environmentPostProcessor.git
git@gitee.com:springboot-source/environmentPostProcessor.git
springboot-source
environmentPostProcessor
自定义environmentPostProcessor
master

搜索帮助