2019独角兽企业重金招聘Python工程师标准>>>
Reason: Property 'url' threw exception; nested exception is java.lang.UnsupportedOperationException
原来的代码:
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setConnectionProperties(connectionProperties);
datasource.setDbType(type);
try {
datasource.setPasswordCallbackClassName(passwordCallbackClassName);
datasource.setFilters(filters);
} catch (Exception e) {
logger.error("druid configuration initialization filter", e);
}
return datasource;
}
解决办法:去掉@ConfigurationProperties(prefix = "spring.datasource") ,不要两次赋值
正确代码
@Primary
@Bean(name = "dataSource")
public DataSource druidDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setConnectionProperties(connectionProperties);
datasource.setDbType(type);
try {
datasource.setPasswordCallbackClassName(passwordCallbackClassName);
datasource.setFilters(filters);
} catch (Exception e) {
logger.error("druid configuration initialization filter", e);
}
return datasource;
}
解决方法:
16:58:11.012 schedule [main] WARN o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Could not bind properties to DruidDataSource (prefix=spring.datasource, ignoreInvalidFields=true, ignoreUnknownFields=true, ignoreNestedProperties=true); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 3 errors
由此日志发现是引起三个错误的原因,然后试着去找prefix=spring.datasource,ignoreNestedProperties等在哪里定义的,然后发现druidDataSource方法有spring.datasource前缀,试着修改了一下ignoreNestedProperties,再去看日志,果然发现是这里的问题