01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.scope;
18:
19: import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
20: import org.springframework.beans.factory.config.BeanDefinition;
21: import org.springframework.beans.factory.config.BeanDefinitionHolder;
22: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
23: import org.springframework.beans.factory.support.RootBeanDefinition;
24:
25: /**
26: * Utility class for creating a scoped proxy.
27: * Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner.
28: *
29: * @author Mark Fisher
30: * @author Juergen Hoeller
31: * @author Rob Harrop
32: * @since 2.5
33: */
34: public abstract class ScopedProxyUtils {
35:
36: private static final String TARGET_NAME_PREFIX = "scopedTarget.";
37:
38: /**
39: * Generates a scoped proxy for the supplied target bean, registering the target
40: * bean with an internal name and setting 'targetBeanName' on the scoped proxy.
41: * @param definition the original bean definition
42: * @param registry the bean definition registry
43: * @param proxyTargetClass whether to create a target class proxy
44: * @return the scoped proxy definition
45: */
46: public static BeanDefinitionHolder createScopedProxy(
47: BeanDefinitionHolder definition,
48: BeanDefinitionRegistry registry, boolean proxyTargetClass) {
49:
50: String originalBeanName = definition.getBeanName();
51: BeanDefinition targetDefinition = definition
52: .getBeanDefinition();
53:
54: // Create a scoped proxy definition for the original bean name,
55: // "hiding" the target bean in an internal target definition.
56: RootBeanDefinition scopedProxyDefinition = new RootBeanDefinition(
57: ScopedProxyFactoryBean.class);
58: scopedProxyDefinition.setSource(definition.getSource());
59:
60: String targetBeanName = TARGET_NAME_PREFIX + originalBeanName;
61: scopedProxyDefinition.getPropertyValues().addPropertyValue(
62: "targetBeanName", targetBeanName);
63:
64: if (proxyTargetClass) {
65: targetDefinition.setAttribute(
66: AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE,
67: Boolean.TRUE);
68: // ScopedFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
69: } else {
70: scopedProxyDefinition.getPropertyValues().addPropertyValue(
71: "proxyTargetClass", Boolean.FALSE);
72: }
73:
74: scopedProxyDefinition.setAutowireCandidate(targetDefinition
75: .isAutowireCandidate());
76: // The target bean should be ignored in favor of the scoped proxy.
77: targetDefinition.setAutowireCandidate(false);
78:
79: // Register the target bean as separate bean in the factory.
80: registry.registerBeanDefinition(targetBeanName,
81: targetDefinition);
82:
83: // Return the scoped proxy definition as primary bean definition
84: // (potentially an inner bean).
85: return new BeanDefinitionHolder(scopedProxyDefinition,
86: originalBeanName, definition.getAliases());
87: }
88:
89: }
|