Spring boot 如何绑定";spring.datasource.tomcat“;spring boot 2中javax.sql.DataSource类的属性
我有一些代码可以在版本2之前的SpringBoot上正常工作,我发现很难将其转换为SpringBoot2 有人能帮忙吗 yml文件:Spring boot 如何绑定";spring.datasource.tomcat“;spring boot 2中javax.sql.DataSource类的属性,spring-boot,spring-data-jpa,tomcat-jdbc,Spring Boot,Spring Data Jpa,Tomcat Jdbc,我有一些代码可以在版本2之前的SpringBoot上正常工作,我发现很难将其转换为SpringBoot2 有人能帮忙吗 yml文件: spring: datasource: type: org.apache.tomcat.jdbc.pool.DataSource tomcat: initial-size: 10 max-active: 20 max-idle: 10 max-wait: 1000
spring:
datasource:
type: org.apache.tomcat.jdbc.pool.DataSource
tomcat:
initial-size: 10
max-active: 20
max-idle: 10
max-wait: 10000
旧代码:
DataSource dataSource = builder.build();
Map<String, Object> properties = null;
try {
RelaxedPropertyResolver relaxedProperty = new RelaxedPropertyResolver(environment);
properties = relaxedProperty
.getSubProperties("spring.datasource.tomcat.");
} catch (Exception e) {
// No need to log an error
}
MutablePropertyValues mutableProperties = new MutablePropertyValues(properties);
if(properties == null || properties.isEmpty()) {
return dataSource;
}
new RelaxedDataBinder(dataSource).bind(mutableProperties);
return dataSource;
如何使用javax.sql.DataSource DataSource=newdatasourcebuilder(this.getClass().getClassLoader()).build();Intellij说“DataSourceBuilder(java.lang.ClassLoader)”在“org.springframework.boot.jdbc.DataSourceBuilder”中具有私有访问权,并更新了代码。请查收。谢谢你必须用空格再次移动“type”和“tomcat”块。哦,这是打字错误,我将在这里编辑。在我的应用程序中,type和tomcat前面有空格。搬动这些东西后你能找到吗?我能够使用binder从yaml中读取这些值,但无法将其映射到数据源类javax.sql.DataSource DataSource=new DataSourceBuilder(this.getClass().getClassLoader()).build();Intellij说“DataSourceBuilder(java.lang.ClassLoader)”在“org.springframework.boot.jdbc.DataSourceBuilder”中具有私有访问权,并更新了代码。请查收。谢谢你必须用空格再次移动“type”和“tomcat”块。哦,这是打字错误,我将在这里编辑。在我的应用程序中,type和tomcat前面有空格。搬动这些东西后你能找到吗?我能够使用活页夹从yaml中读取这些值,但无法将其映射到数据源类
DataSourceBuilder<?> dataSourceBuilder =
DataSourceBuilder.create(this.getClass().getClassLoader());
javax.sql.DataSource dataSource = dataSourceBuilder.build();
Map<String, Object> properties = null;
try {
properties = Binder.get(environment)
.bind("spring.datasource.tomcat", Bindable.mapOf(String.class, Object.class)).orElseGet(Collections::emptyMap);
} catch (Exception e) {
// No need to log an error
}
if(properties == null || properties.isEmpty()) {
return dataSource;
}
return Binder.get(environment)
.bind("spring.datasource.tomcat", DataSource.class).get();
java.util.NoSuchElementException: No value bound
at org.springframework.boot.context.properties.bind.BindResult.get(BindResult.java:56)