@configurationproperties是springboot新加入的注解,主要用于配置文件中的指定键值对映射到一个java实体类上。那么它是怎么发挥作用的呢?下面我们将揭开@configurationproperties的魔法。

configurationpropertiesbindingpostprocessor这个bean后置处理器,就是来处理bean属性的绑定的,这个bean后置处理器后文将称之为properties后置处理器。你需要知道以下几件事:

ioc容器context的enviroment.propertysources记录着系统属性、应用属性以及springboot的默认配置文件application.properties中的配置属性等。properties后置处理器就是从其中找到匹配的配置项绑定到bean的属性上去的。
属性绑定是有覆盖性的,操作系统环境变量可以覆盖配置文件application.properties, java系统属性可以覆盖操作系统环境变量。更多的可以参考官网 https://docs.spring.io/spring-boot/docs/2.1.1.release/reference/htmlsingle/#boot-features-external-config

重点给大家介绍springboot配置数据源无法读取配置信息的问题及解决方案,具体内容如下:

出现的问题:

利用configurationproperties注解配置数据源发现读取的数据库配置信息全部为null。

@bean(name = "pq")
    @configurationproperties(prefix = "spring.datasource")
    public datasource datasourcepq() {
        return datasourcebuilder.create().build();     
    }
#配置信息
spring:
  datasource:
    type: com.alibaba.druid.pool.druiddatasource
    #mysql配置
    driverclassname: com.mysql.jdbc.driver
    url: jdbc:mysql://127.0.0.1:3306/graduate?useunicode=true&characterencoding=utf-8&usessl=false
    username: root
    password: root

debug过后发现配置文件属性没有读取成功

解决方法:

@bean(name = "pq")
    @configurationproperties(prefix = "spring.datasource")
    public datasource datasourcepq() {
        //return datasourcebuilder.create().build();
        return new druiddatasource();
    }

出现问题原因:

暂时还不清楚,第一次使用这样的方式配置数据源,可能有些细节没有注意到。

以上就是解决springboot利用configurationproperties注解配置数据源无法读取配置信息问题的详细内容,更多关于springboot配置数据源无法读取配置信息的资料请关注www.887551.com其它相关文章!