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

Uses RuntimeBeanReference to prevent the creation of InnerBean on spring namespace #489

Closed
zhouyougit opened this issue Dec 11, 2017 · 3 comments

Comments

@zhouyougit
Copy link

zhouyougit commented Dec 11, 2017

版本信息

sharding-jdbc:1.5.1
spring-boot: 1.5.9
druid:1.1.6
druid-spring-boot-starter:1.1.6

现象

使用sharding-jdbc的namespace的配置方式时,rdb:sharding-rule里指定的data-sources使用的并不是context里已有的datasource bean,而是自行创建的一个inner datasource bean。
同时翻阅了一下sharding-jdbc 2.0.0里的starter,发现也是会读取配置文件,然后直接自行构建datasource。

问题

  1. 为何总是独立创建datasource,是有什么特殊考虑么,而不是用context中现有的datasource bean么?
  2. 另一个问题是兼容性问题,当使用了druid-spring-boot-starter,这个starter里的多数据源配置是基于spring 的FactoryBeanPostProcessor + configurationProperties来实现的,而spring里有个机制是inner bean不执行FactoryBeanPostProcessor,从而导致sharding-jdbc里的inner datasource bean无法正常初始化。
@haocao
Copy link
Member

haocao commented Dec 11, 2017

应该没有自行创建bean,dataSources是通过parserContext.getRegistry().getBeanDefinition(each)构建出来的。

private Map<String, BeanDefinition> parseDataSources(final Element element, final ParserContext parserContext) {
        Element shardingRuleElement = DomUtils.getChildElementByTagName(element, ShardingDataSourceBeanDefinitionParserTag.SHARDING_RULE_CONFIG_TAG);
        List<String> dataSources = Splitter.on(",").trimResults().splitToList(shardingRuleElement.getAttribute(ShardingDataSourceBeanDefinitionParserTag.DATA_SOURCE_NAMES_TAG));
        Map<String, BeanDefinition> result = new ManagedMap<>(dataSources.size());
        for (String each : dataSources) {
            result.put(each, parserContext.getRegistry().getBeanDefinition(each));
        }
        return result;
    }

image

所谓的inner datasource bean是指什么呢?

@zhouyougit
Copy link
Author

@haocao 可以看看spring的BeanDefinitionValueResolver这个类的resolveValueIfNecessary方法,
当创建ShardingDataSource的时候处理datasource字段时会调用到这个方法,从方法的注释可以看到如果类型是BeanDefinition那么会创建一个anonymous prototypes的Inner beans.
说道的inner datasource bean就是指的这里创建的类型为Inner beans的datasource
image

@haocao haocao closed this as completed in 829bb00 Dec 12, 2017
@terrymanu terrymanu changed the title 请教一个关于sharding-jdbc总是自行创建数据源的问题 Uses RuntimeBeanReference to prevent the creation of InnerBean on spring namespace Aug 8, 2018
@terrymanu
Copy link
Member

fixed at 2.0.1

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

No branches or pull requests

3 participants