KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > config > ScopedProxyBeanDefinitionDecorator


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.aop.config;
18
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21
22 import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
23 import org.springframework.aop.scope.ScopedProxyFactoryBean;
24 import org.springframework.beans.factory.config.BeanDefinition;
25 import org.springframework.beans.factory.config.BeanDefinitionHolder;
26 import org.springframework.beans.factory.support.AbstractBeanDefinition;
27 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
28 import org.springframework.beans.factory.support.RootBeanDefinition;
29 import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
30 import org.springframework.beans.factory.xml.ParserContext;
31
32 /**
33  * {@link BeanDefinitionDecorator} responsible for parsing the
34  * <code>&lt;aop:scoped-proxy/&gt;</code> tag.
35  *
36  * @author Rob Harrop
37  * @author Juergen Hoeller
38  * @since 2.0
39  */

40 class ScopedProxyBeanDefinitionDecorator implements BeanDefinitionDecorator {
41
42     private static final String JavaDoc PROXY_TARGET_CLASS = "proxy-target-class";
43
44     private static final String JavaDoc TARGET_NAME_PREFIX = "scopedTarget.";
45
46
47     public BeanDefinitionHolder decorate(Node JavaDoc node, BeanDefinitionHolder definition, ParserContext parserContext) {
48         String JavaDoc originalBeanName = definition.getBeanName();
49         BeanDefinition targetDefinition = definition.getBeanDefinition();
50         BeanDefinitionRegistry registry = parserContext.getRegistry();
51
52         // Create a scoped proxy definition for the original bean name,
53
// "hiding" the target bean in an internal target definition.
54
String JavaDoc targetBeanName = TARGET_NAME_PREFIX + originalBeanName;
55         RootBeanDefinition scopedProxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class);
56         scopedProxyDefinition.getPropertyValues().addPropertyValue("targetBeanName", targetBeanName);
57
58         boolean proxyTargetClass = (!(node instanceof Element JavaDoc) ||
59                 Boolean.valueOf(((Element JavaDoc) node).getAttribute(PROXY_TARGET_CLASS)).booleanValue());
60         if (proxyTargetClass) {
61             targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
62             // ScopedFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
63
}
64         else {
65             scopedProxyDefinition.getPropertyValues().addPropertyValue("proxyTargetClass", Boolean.FALSE);
66         }
67
68         // The target bean should be ignored in favor of the scoped proxy.
69
if (targetDefinition instanceof AbstractBeanDefinition) {
70             ((AbstractBeanDefinition) targetDefinition).setAutowireCandidate(false);
71         }
72
73         // Register the target bean as separate bean in the factory.
74
registry.registerBeanDefinition(targetBeanName, targetDefinition);
75
76         // Return the scoped proxy definition as primary bean definition
77
// (potentially an inner bean).
78
return new BeanDefinitionHolder(scopedProxyDefinition, originalBeanName);
79     }
80
81 }
82
Popular Tags