zoukankan      html  css  js  c++  java
  • SpringBoot 自定义配置

    有时候需要自己定义一些配置,比如SpringBoot没有提供Druid连接池的配置,需要我们自己写配置。

    以在springboot中使用Druid为例。

    依赖

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.8</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.19</version>
            </dependency>

    配置文件

    #指定数据源类型为Druid
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    
    ##########druid连接池配置#########
    spring.datasource.druid.url=jdbc:mysql://127.0.0.1/db_xm_mall?serverTimezone=UTC
    spring.datasource.druid.driverClassName=com.mysql.cj.jdbc.Driver
    spring.datasource.druid.username=chy
    spring.datasource.druid.password=abcd
    ##初始连接数,默认0
    spring.datasource.druid.initialSize=10
    #最大连接数,默认8
    spring.datasource.druid.maxActive=30
    spring.datasource.druid.minIdle=10
    #获取连接的最大等待时间,单位毫秒
    spring.datasource.druid.maxWait=2000
    #缓存PreparedStatement,默认false
    #spring.datasource.druid.poolPreparedStatements=true
    #缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句设置
    spring.datasource.druid.maxOpenPreparedStatements=20

    配置类

    /**
     * 初始化Druid连接池
     */
    @ConfigurationProperties("spring.datasource.druid")  //指定前缀
    @Component  //也可以使用@Configuration,@Configuration包含了@Component
    @Setter  //Lombok的注解。流程是调用空参构造器创建对象,再调用setter方法注入属性值,也可以使用@Data
    public class DruidConfig {
        //属性名要与springboot配置文件中属性名一致
        private String url;
        private String driverClassName;
        private String username;
        private String password;
        private int initialSize;
        private int maxActive;
        private int minIdle;
        private int maxWait;
        // private boolean poolPreparedStatements;
        private int maxOpenPreparedStatements;
    
        @Bean  //放到Spring容器中
        // @Primary  //声明为主数据源,如果配置了多个数据源,未显式指定使用哪个数据源时,自动使用主数据源
        public DataSource dataSource() {
            DruidDataSource datasource = new DruidDataSource();
    
            datasource.setUrl(url);
            datasource.setDriverClassName(driverClassName);
            datasource.setUsername(username);
            datasource.setPassword(password);
            datasource.setInitialSize(initialSize);
            datasource.setMaxActive(maxActive);
            datasource.setMinIdle(minIdle);
            datasource.setMaxWait(maxWait);
            // datasource.setPoolPreparedStatements(poolPreparedStatements);
            datasource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
    
            return datasource;
        }
    
    }

    三点:

    • 指定前缀。如果不需要指定前缀,可以省略这一步
    • 提供空参构造器、setter方法
    • @Component放到Spring容器中

    如果要写配置文件时有相应的提示,需要加一个依赖

            <!-- 自定义配置的提示 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>

    编译,target/classes/META-INF下会生成一个spring-configuration-metadata.json文件,在配置文件中写相关配置时就有提示啦(如果已存在该配置项,则不会提示)。

  • 相关阅读:
    如何将baidu地图中的baidu logo 去掉
    漂亮的圆角文本框 CSS3实现
    jQuery数字加减插件
    腾迅股票数据接口 http/javascript
    JS复制内容到剪贴板(兼容FF/Chrome/Safari所有浏览器)
    关于编写性能高效的javascript事件的技术
    想做web开发 就学JavaScript
    git的简单理解及基础操作命令
    《CSS权威指南》基础复习+查漏补缺
    TypeScript Writing .d.ts files(编写声明文件)
  • 原文地址:https://www.cnblogs.com/chy18883701161/p/12685833.html
Copyright © 2011-2022 走看看