sharding-jdbc不分片策略NoneShardingStrategy在springboot中的写法

使用springboot的application.properties来定义分片策略时,

sharding.jdbc.config.sharding.default-data -strategy.none=

这样写会报错

***************************APPLICATION FAILED TO START***************************De ion:Binding to target io.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties@675ffd1d failed:    Property: sharding.jdbc.config.sharding.defaultData Strategy.none    Value:     Reason: Failed to convert property value of type 'java.lang.String' to required type 'io.shardingjdbc.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration' for property 'defaultData Strategy.none'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'io.shardingjdbc.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration' for property 'none': no matching editors or conversion strategy foundAction:Update your application's configuration

需要写成

sharding.jdbc.config.sharding.default-data -strategy.none.any=

即,在none后面再加一层随便什么名字。

因为springboot中,使用了@ConfigurationProperties的类,在properties文件中就可以通过 “prefix前缀 . 成员变量名 = 值” 来配置。

如果成员变量还是一个类,就再加一层来配置。即“prefix前缀 . 成员变量名 . 子类成员变量名 = 值”。

特殊的, 如果成员变量是一个类,而它却没有成员变量了(例如amlNoneShardingStrategyConfiguration),那么仍然需要加一层。即“prefix前缀 . 成员变量名 . 任意名 = 值”。

// SpringBootShardingRuleConfigurationProperties.java@ConfigurationProperties(prefix = "sharding.jdbc.config.sharding")public class SpringBootShardingRuleConfigurationProperties extends YamlShardingRuleConfiguration {}// YamlNoneShardingStrategyConfiguration.javapublic final class YamlNoneShardingStrategyConfiguration implements YamlShardingStrategyConfiguration {}
收藏 打印