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