• Spring Boot中注入配置文件application.properties中的list 对象参数


    例如要注入下列参数:

    dyn.spring.datasources[0].name=branchta
    dyn.spring.datasources[0].driverClassName=oracle.jdbc.OracleDriver
    dyn.spring.datasources[0].url=jdbc:oracle:thin:@IP:1521:ora11g
    dyn.spring.datasources[0].username=scott
    dyn.spring.datasources[0].password=tiger
    dyn.spring.datasources[1].name=ta4
    dyn.spring.datasources[1].driverClassName=oracle.jdbc.OracleDriver
    dyn.spring.datasources[1].url=jdbc:oracle:thin:@IP:1521:ora11g
    dyn.spring.datasources[1].username=hr
    dyn.spring.datasources[1].password=hr

    这样,通过结合RootBeanDefinition以及DefaultListableBeanFactory相关接口,就可以纯编程式动态创建bean,而无需修改xml或者配置bean,达到纯动态的目的。

    但是网上的例子似乎都是注入string类型的list,经测试,只要将datasources包含的类型定义为public即可,如下:

    package com.XX.XXX.datasource;
    
    import java.util.List;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    
    @ConfigurationProperties(prefix="dyn.spring")
    @PropertySource("classpath:jrescloud.properties")
    public class DynamicDataSourceConfig {
        
        private List<DataSource> datasources;
        
        public static class DataSource {
            private String name;
            private String driverClassName;
            private String url;
            private String username;
            private String password;
            private String maxActive;
            private String maxIdle;
            private String maxWait;
            private String validationQuery;
            private String defaultAutoCommit;
            private String connectionInitSqls;
            
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public String getDriverClassName() {
                return driverClassName;
            }
            public void setDriverClassName(String driverClassName) {
                this.driverClassName = driverClassName;
            }
            public String getUrl() {
                return url;
            }
            public void setUrl(String url) {
                this.url = url;
            }
            public String getUsername() {
                return username;
            }
            public void setUsername(String username) {
                this.username = username;
            }
            public String getPassword() {
                return password;
            }
            public void setPassword(String password) {
                this.password = password;
            }
            public String getMaxActive() {
                return maxActive;
            }
            public void setMaxActive(String maxActive) {
                this.maxActive = maxActive;
            }
            public String getMaxIdle() {
                return maxIdle;
            }
            public void setMaxIdle(String maxIdle) {
                this.maxIdle = maxIdle;
            }
            public String getMaxWait() {
                return maxWait;
            }
            public void setMaxWait(String maxWait) {
                this.maxWait = maxWait;
            }
            public String getValidationQuery() {
                return validationQuery;
            }
            public void setValidationQuery(String validationQuery) {
                this.validationQuery = validationQuery;
            }
            public String getDefaultAutoCommit() {
                return defaultAutoCommit;
            }
            public void setDefaultAutoCommit(String defaultAutoCommit) {
                this.defaultAutoCommit = defaultAutoCommit;
            }
            public String getConnectionInitSqls() {
                return connectionInitSqls;
            }
            public void setConnectionInitSqls(String connectionInitSqls) {
                this.connectionInitSqls = connectionInitSqls;
            }
        }
    
        public List<DataSource> getDatasources() {
            return datasources;
        }
    
        public void setDatasources(List<DataSource> datasources) {
            this.datasources = datasources;
        }
    }

    这样就可以注入了。。。。。:)

  • 相关阅读:
    Flask--Config研究
    Python和Mongodb
    使用 wsgiref 创建WSGI APP
    如何使用werkzeug创建WSGI APP
    编程常用数据结构与函数总结(vector、list、stack、deque、字符串)
    LeetCode 841 钥匙和房间(怎么减少程序运行时间、BFS、lambda、sync_with_stdio、cin.tie)
    卡尔曼滤波总结
    ROS之rviz显示历史运动轨迹、路径的各种方法(visualization_msgs/Marker、nav_msgs/Path)
    二叉树前序遍历、中序遍历、后序遍历、层序遍历的直观理解
    MathType常用快捷键
  • 原文地址:https://www.cnblogs.com/zhjh256/p/9948549.html
Copyright © 2020-2023  润新知