• SpringBoot入门三(java代码方式配置之使用@ConfigurationProperties实现Spring Boot配置文件配置项读取)


    目的:能够使用@ConfigurationProperties实现Spring Boot配置文件配置项读取(对象中)和应用

    分析:
    需求:将配置文件中的配置项读取到一个对象中;
    实现:可以使用Spring Boot提供的注解@ConfigurationProperties,该注解可以将Spring Boot的配置文件(默认必须application.properties或application.yml)中的配置项读取到一个对象中

    实现步骤:

    1.加入注解依赖(不加入依赖@ConfigurationProperties会报错)
    2.创建配置项类(jdbcProperties类),在改类名上添加注解@ConfigurationProperties,就可以把配置文件读取到此类上.
    3.将jdbc.properties修改名称为application.properties
    4.将jdbcProperites对象注入到JdbcConfig
    5.测试

    ===========================

    1.加入注解依赖(不加入依赖@ConfigurationProperties会报错)

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <!--SpringBoot父工程-->
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.3.4.RELEASE</version>
        </parent>
    
        <groupId>org.cc8w</groupId>
        <artifactId>javaboot_test</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!--java编译版本,父工程已经指定了这样的变量-->
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!--SpringBoot Web启动器(不用写版本跟随父版本)-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--在配置注解@ConfigurationProperties加的-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.20</version>
            </dependency>
    
        </dependencies>
    
    </project>


    2.创建配置项类(jdbcProperties类),在改类名上添加注解@ConfigurationProperties,就可以把配置文件读取到此类上.

    package com.cc8w.config;
    
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * @ConfigurationProperties从application配置文件中读取配置项
     * prefix 表示配置项的前缀,配置了就不用写前缀了
     * 配置项类中的类变量必须要和前缀之后的配置项名称一致
     */
    
    @ConfigurationProperties(prefix = "jdbc")
    public class JdbcProperties {
    
        private String driverClassName;
        private String url;
        private String username;
        private String password;
    
        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;
        }
    }


    3.将jdbc.properties修改名称为application.properties (略)

    4.将jdbcProperites对象注入到JdbcConfig

    package com.cc8w.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import javax.sql.DataSource;
    
    @Configuration
    //@PropertySource("classpath:jdbc.properties")
    @EnableConfigurationProperties(JdbcProperties.class)
    public class JdbcConfig {
    /* 上一个案例写的
        @Value("${jdbc.driverClassName}")
        private String driverClassName;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
    
        @Bean
        public DataSource dataSource(){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            return dataSource;
        }
    */
    
    
        @Bean
        public DataSource dataSource(JdbcProperties jdbcProperties){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
            dataSource.setUrl(jdbcProperties.getUrl());
            dataSource.setUsername(jdbcProperties.getUsername());
            dataSource.setPassword(jdbcProperties.getPassword());
            return dataSource;
        }
    
    }

    5.目录结构和测试

  • 相关阅读:
    原生js,实现跨越的jsonp
    nodejs+express+jade安装步骤
    浏览器端创建可下载文件
    保留有效数字
    日期转换格式
    js添加千分位
    vue 中的 全选和 反选
    防止域名被恶意解析
    node命令行工具之实现项目工程自动初始化的标准流程
    React router动态加载组件-适配器模式的应用
  • 原文地址:https://www.cnblogs.com/fps2tao/p/13816185.html
Copyright © 2020-2023  润新知