Search in sources :

Example 1 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcDataSourceBeanDefinitionParser method parseTableRuleConfig.

private BeanDefinition parseTableRuleConfig(final Element tableElement) {
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(TableRuleConfig.class);
    String dynamic = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DYNAMIC_TABLE_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(dynamic)) {
        factory.addPropertyValue("dynamic", dynamic);
    }
    String actualTables = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.ACTUAL_TABLES_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(actualTables)) {
        factory.addPropertyValue("actualTables", actualTables);
    }
    String dataSourceNames = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATA_SOURCE_NAMES_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(dataSourceNames)) {
        factory.addPropertyValue("dataSourceNames", dataSourceNames);
    }
    String databaseStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATABASE_STRATEGY_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(databaseStrategy)) {
        factory.addPropertyReference("databaseStrategy", databaseStrategy);
    }
    String tableStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.TABLE_STRATEGY_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(tableStrategy)) {
        factory.addPropertyReference("tableStrategy", tableStrategy);
    }
    List<Element> autoIncrementColumns = DomUtils.getChildElementsByTagName(tableElement, ShardingJdbcDataSourceBeanDefinitionParserTag.AUTO_INCREMENT_COLUMN);
    if (null == autoIncrementColumns || autoIncrementColumns.isEmpty()) {
        return factory.getBeanDefinition();
    }
    factory.addPropertyValue("autoIncrementColumns", Lists.transform(autoIncrementColumns, new Function<Element, AutoIncrementColumnConfig>() {

        @Override
        public AutoIncrementColumnConfig apply(final Element input) {
            AutoIncrementColumnConfig result = new AutoIncrementColumnConfig();
            result.setColumnName(input.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_NAME));
            result.setColumnIdGeneratorClass(input.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_ID_GENERATOR_CLASS));
            return result;
        }
    }));
    return factory.getBeanDefinition();
}
Also used : Function(com.google.common.base.Function) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AutoIncrementColumnConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig) Element(org.w3c.dom.Element)

Example 2 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcDataSourceBeanDefinitionParser method parseInternal.

@Override
protected //CHECKSTYLE:OFF
AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) {
    //CHECKSTYLE:ON
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(SpringShardingDataSource.class);
    factory.addConstructorArgValue(parseShardingRuleConfig(element, parserContext));
    factory.addConstructorArgValue(parseProperties(element, parserContext));
    return factory.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 3 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcDataSourceBeanDefinitionParser method parseShardingRuleConfig.

private BeanDefinition parseShardingRuleConfig(final Element element, final ParserContext parserContext) {
    Element shardingRuleElement = DomUtils.getChildElementByTagName(element, ShardingJdbcDataSourceBeanDefinitionParserTag.SHARDING_RULE_CONFIG_TAG);
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(ShardingRuleConfig.class);
    factory.addPropertyValue("dataSource", parseDataSources(shardingRuleElement, parserContext));
    parseDefaultDataSource(factory, shardingRuleElement);
    factory.addPropertyValue("tables", parseTableRulesConfig(shardingRuleElement));
    factory.addPropertyValue("bindingTables", parseBindingTablesConfig(shardingRuleElement));
    factory.addPropertyValue("defaultDatabaseStrategy", parseDefaultDatabaseStrategyConfig(shardingRuleElement));
    factory.addPropertyValue("defaultTableStrategy", parseDefaultTableStrategyConfig(shardingRuleElement));
    parseIdGenerator(factory, shardingRuleElement);
    return factory.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Element(org.w3c.dom.Element)

Example 4 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcDataSourceBeanDefinitionParser method parseBindingTablesConfig.

private List<BeanDefinition> parseBindingTablesConfig(final Element element) {
    Element bindingTableRulesElement = DomUtils.getChildElementByTagName(element, ShardingJdbcDataSourceBeanDefinitionParserTag.BINDING_TABLE_RULES_TAG);
    if (null == bindingTableRulesElement) {
        return Collections.emptyList();
    }
    List<Element> bindingTableRuleElements = DomUtils.getChildElementsByTagName(bindingTableRulesElement, ShardingJdbcDataSourceBeanDefinitionParserTag.BINDING_TABLE_RULE_TAG);
    BeanDefinitionBuilder bindingTableRuleFactory = BeanDefinitionBuilder.rootBeanDefinition(BindingTableRuleConfig.class);
    List<BeanDefinition> result = new ManagedList<>(bindingTableRuleElements.size());
    for (Element bindingTableRuleElement : bindingTableRuleElements) {
        bindingTableRuleFactory.addPropertyValue("tableNames", bindingTableRuleElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.LOGIC_TABLES_ATTRIBUTE));
        result.add(bindingTableRuleFactory.getBeanDefinition());
    }
    return result;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcStrategyBeanDefinition method getBeanDefinitionByElement.

static AbstractBeanDefinition getBeanDefinitionByElement(final Element element) {
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(StrategyConfig.class);
    factory.addPropertyValue("shardingColumns", element.getAttribute(ShardingJdbcStrategyBeanDefinitionParserTag.SHARDING_COLUMNS_ATTRIBUTE));
    factory.addPropertyValue("algorithmClassName", element.getAttribute(ShardingJdbcStrategyBeanDefinitionParserTag.ALGORITHM_CLASS_ATTRIBUTE));
    factory.addPropertyValue("algorithmExpression", element.getAttribute(ShardingJdbcStrategyBeanDefinitionParserTag.ALGORITHM_EXPRESSION_ATTRIBUTE));
    return factory.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Aggregations

BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)539 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)115 Element (org.w3c.dom.Element)99 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)60 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)59 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)53 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 ManagedList (org.springframework.beans.factory.support.ManagedList)33 ManagedMap (org.springframework.beans.factory.support.ManagedMap)29 HashMap (java.util.HashMap)19 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)19 Test (org.junit.jupiter.api.Test)16 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)16 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)12 Node (org.w3c.dom.Node)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)10 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)10