spring事物管理源码分析2

 2019-11-02 21:28  阅读(1007)
文章分类:Spring boot
TxNamespaceHandler
    @Override
        public void init() {
            registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
            registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
            registerBeanDefinitionParser("jta-transaction-manager", new JtaTransactionManagerBeanDefinitionParser());
        }

TxAdviceBeanDefinitionParser继承AbstractBeanDefinitionParser
    @Override
        public final BeanDefinition parse(Element element, ParserContext parserContext) {
            AbstractBeanDefinition definition = parseInternal(element, parserContext);
            if (definition != null && !parserContext.isNested()) {
                try {
                    String id = resolveId(element, definition, parserContext);
                    if (!StringUtils.hasText(id)) {
                        parserContext.getReaderContext().error(
                                "Id is required for element '" + parserContext.getDelegate().getLocalName(element)
                                        + "' when used as a top-level tag", element);
                    }
                    String[] aliases = new String[0];
                    String name = element.getAttribute(NAME_ATTRIBUTE);
                    if (StringUtils.hasLength(name)) {
                        aliases = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
                    }
                    BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id, aliases);
                    registerBeanDefinition(holder, parserContext.getRegistry());
                    if (shouldFireEvents()) {
                        BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder);
                        postProcessComponentDefinition(componentDefinition);
                        parserContext.registerComponent(componentDefinition);
                    }
                }
                catch (BeanDefinitionStoreException ex) {
                    parserContext.getReaderContext().error(ex.getMessage(), element);
                    return null;
                }
            }
            return definition;
        }

AbstractSingleBeanDefinitionParser
    @Override
        protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
            String parentName = getParentName(element);
            if (parentName != null) {
                builder.getRawBeanDefinition().setParentName(parentName);
            }
            Class<?> beanClass = getBeanClass(element);
            if (beanClass != null) {
                builder.getRawBeanDefinition().setBeanClass(beanClass);
            }
            else {
                String beanClassName = getBeanClassName(element);
                if (beanClassName != null) {
                    builder.getRawBeanDefinition().setBeanClassName(beanClassName);
                }
            }
            builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
            if (parserContext.isNested()) {
                // Inner bean definition must receive same scope as containing bean.
                builder.setScope(parserContext.getContainingBeanDefinition().getScope());
            }
            if (parserContext.isDefaultLazyInit()) {
                // Default-lazy-init applies to custom bean definitions as well.
                builder.setLazyInit(true);
            }
            doParse(element, parserContext, builder);
            return builder.getBeanDefinition();
        }

TxAdviceBeanDefinitionParser
    @Override
        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
            builder.addPropertyReference("transactionManager", TxNamespaceHandler.getTransactionManagerName(element));

            List<Element> txAttributes = DomUtils.getChildElementsByTagName(element, ATTRIBUTES_ELEMENT);
            if (txAttributes.size() > 1) {
                parserContext.getReaderContext().error(
                        "Element <attributes> is allowed at most once inside element <advice>", element);
            }
            else if (txAttributes.size() == 1) {
                // Using attributes source.
                Element attributeSourceElement = txAttributes.get(0);
                RootBeanDefinition attributeSourceDefinition = parseAttributeSource(attributeSourceElement, parserContext);
                builder.addPropertyValue("transactionAttributeSource", attributeSourceDefinition);
            }
            else {
                // Assume annotations source.
                builder.addPropertyValue("transactionAttributeSource",
                        new RootBeanDefinition("org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"));
            }
        }

    protected Class<?> getBeanClass(Element element) {
      return TransactionInterceptor.class;
     }

来源:http://ddrv.cn

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> spring事物管理源码分析2

相关推荐