KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > config > AnnotationDrivenBeanDefinitionParser


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.transaction.config;
18
19 import org.w3c.dom.Element JavaDoc;
20
21 import org.springframework.aop.config.AopNamespaceUtils;
22 import org.springframework.beans.factory.config.RuntimeBeanReference;
23 import org.springframework.beans.factory.support.AbstractBeanDefinition;
24 import org.springframework.beans.factory.support.RootBeanDefinition;
25 import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
26 import org.springframework.beans.factory.xml.ParserContext;
27 import org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor;
28 import org.springframework.transaction.interceptor.TransactionInterceptor;
29
30 /**
31  * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} implementation that
32  * allows users to easily configure all the infrastructure beans required to enable
33  * annotation-driven transaction demarcation.
34  *
35  * <p>By default, all proxies are created as JDK proxies. This may cause some problems if
36  * you are injecting objects as concrete classes rather than interfaces. To overcome this
37  * restriction you can set the '<code>proxy-target-class</code>' attribute to '<code>true</code>'.
38  *
39  * @author Rob Harrop
40  * @author Juergen Hoeller
41  * @since 2.0
42  */

43 class AnnotationDrivenBeanDefinitionParser extends AbstractBeanDefinitionParser {
44
45     /**
46      * Bean property name for injecting the {@link TransactionInterceptor}.
47      */

48     private static final String JavaDoc TRANSACTION_INTERCEPTOR = "transactionInterceptor";
49
50     /** The '<code>proxy-target-class</code>' attribute */
51     private static final String JavaDoc PROXY_TARGET_CLASS = "proxy-target-class";
52
53     /** The '<code>order</code>' property/attribute */
54     private static final String JavaDoc ORDER = "order";
55
56
57     /**
58      * Parses the '<code>&lt;tx:annotation-driven/>&gt;</code>' tag. Will
59      * {@link AopNamespaceUtils#registerAutoProxyCreatorIfNecessary register an AutoProxyCreator} in
60      * the container as necessary.
61      */

62     protected AbstractBeanDefinition parseInternal(Element JavaDoc element, ParserContext parserContext) {
63         AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);
64
65         boolean proxyTargetClass = Boolean.valueOf(element.getAttribute(PROXY_TARGET_CLASS)).booleanValue();
66         if (proxyTargetClass) {
67             AopNamespaceUtils.forceAutoProxyCreatorToUseClassProxying(parserContext.getRegistry());
68         }
69
70         String JavaDoc transactionManagerName = element.getAttribute(TxNamespaceUtils.TRANSACTION_MANAGER_ATTRIBUTE);
71         Class JavaDoc sourceClass = TxNamespaceUtils.getAnnotationTransactionAttributeSourceClass();
72
73         // Create the TransactionInterceptor definition.
74
RootBeanDefinition interceptorDefinition = new RootBeanDefinition(TransactionInterceptor.class);
75         interceptorDefinition.setSource(parserContext.extractSource(element));
76         interceptorDefinition.getPropertyValues().addPropertyValue(
77                 TxNamespaceUtils.TRANSACTION_MANAGER_PROPERTY, new RuntimeBeanReference(transactionManagerName));
78         interceptorDefinition.getPropertyValues().addPropertyValue(
79                 TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE, new RootBeanDefinition(sourceClass));
80
81         // Create the TransactionAttributeSourceAdvisor definition.
82
RootBeanDefinition advisorDefinition = new RootBeanDefinition(TransactionAttributeSourceAdvisor.class);
83         advisorDefinition.setSource(parserContext.extractSource(element));
84         advisorDefinition.getPropertyValues().addPropertyValue(TRANSACTION_INTERCEPTOR, interceptorDefinition);
85         if (element.hasAttribute(ORDER)) {
86             advisorDefinition.getPropertyValues().addPropertyValue(
87                     ORDER, element.getAttribute(ORDER));
88         }
89         return advisorDefinition;
90     }
91
92     protected boolean shouldGenerateId() {
93         return true;
94     }
95
96 }
97
Popular Tags