Spring boot failed to read .yml

1, application.yml

spring:
  profiles:
    active: dev
  datasource:
    url: jdbc:mysql://192.168.2.201:3306/pmsd?useUnicode=true&characterEncoding=utf8&ssl=false
    username: app_pmsd
    password: 635151_Itp
    driver-class-name: com.mysql.jdbc.Driver
    druid:
      initial-size: 1
      minIdle: 5
      maxActive: 20
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000

2, DataSourceProperties.java

@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
    private String url;
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
}

3, MybatisConfigurer.java

@Configuration
public class MybatisConfigurer {
    @Autowired
    private DataSourceProperties dataSourceProperties;
    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(dataSourceProperties.getUrl());
        //druidDataSource.setUsername(dataSourceProperties.getUserName());
        //druidDataSource.setPassword(dataSourceProperties.getPassWord());
        //druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
        return druidDataSource;
    }
   }

4, pom.xml has references

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
  </dependency>
The

code is like the above. I got null, when I got dataSourceProperties. Excuse me, where did the gods write it wrong? Thank you

Mar.28,2021

add code to DataSourceProperties

 public DataSource config(DruidDataSource dataSource){
        dataSource.setUrl(url);
        return dataSource;
 }
Change it to

in

MybatisConfigurer

    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

has been resolved. The reason is that I have another method, MapperScannerConfigurer, in the MybatisConfigurer class, because spring executes the MapperScannerConfigurer method first, and the sqlsessionFactory, has not been created at the time of execution, so it will cause a null pointer exception

Menu