Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当使用starter配置多数据源时,ConfigurationProperties的rebinder事件会导致异常 #2178

Open
zhouyougit opened this issue Dec 11, 2017 · 4 comments

Comments

@zhouyougit
Copy link

环境版本:

  • druid-spring-boot-starter: 1.1.6
  • spring-boot:1.5.9.RELEASE

复现方法

前提

配置多数据源: spring.datasource.druid.*的默认配置与spring.datasource.druid.XXX.*的配置不一致,并且是使用下面ConfigurationProperties的配置方式来配置时

@Bean
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSourceTwo(){
    return DruidDataSourceBuilder.create().build();
}
触发原因

当该DataSource已经初始化一次之后,如果Spring再次触发EnvironmentChangeEvent事件,会导致使用所有标注了@ConfigurationProperties的DruidDataSourceWrapper重新做参数绑定。这个时候,根据spring的规则,会先绑定DruidDataSourceWrapper对象class上标注的@ConfigurationProperties,而这个参数和factory方法上绑定的参数不一致,这时候就会触发DruidDataSource内对已经inited之后修改属性值抛出的UnsupportOperationException

异常示例
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors
Field error in object 'spring.datasource.druid' on field 'url': rejected value [jdbc:mysql://XXXXXX]; codes [methodInvocation.spring.datasource.druid.url,methodInvocation.url,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.datasource.druid.url,url]; arguments []; default message [url]]; default message [Property 'url' threw exception; nested exception is java.lang.UnsupportedOperationException]
	at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359)
	at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276)
	at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
	at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:330)
	... 27 more
@lihengming
Copy link
Contributor

这种继承的方式和 Spring Cloud Config 动态刷新配置不太兼容,我研究下解决方案。

@fangzuoxian
Copy link

@zhouyougit 我也遇到这个问题,多数据源报Property 'url' threw exception; nested exception is java.lang.UnsupportedOperationException,请问怎么解决的?

@davelet
Copy link

davelet commented Apr 26, 2019

现在啥进展了

1 similar comment
@private14
Copy link

现在啥进展了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants