1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
24
24
import org .springframework .beans .factory .support .RootBeanDefinition ;
25
25
import org .springframework .lang .Nullable ;
26
+ import org .springframework .util .Assert ;
26
27
27
28
/**
28
29
* Utility class for creating a scoped proxy.
29
- * Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner.
30
+ *
31
+ * <p>Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner.
30
32
*
31
33
* @author Mark Fisher
32
34
* @author Juergen Hoeller
33
35
* @author Rob Harrop
36
+ * @author Sam Brannen
34
37
* @since 2.5
35
38
*/
36
39
public abstract class ScopedProxyUtils {
37
40
38
41
private static final String TARGET_NAME_PREFIX = "scopedTarget." ;
39
42
43
+ private static final int TARGET_NAME_PREFIX_LENGTH = TARGET_NAME_PREFIX .length ();
44
+
40
45
41
46
/**
42
47
* Generate a scoped proxy for the supplied target bean, registering the target
@@ -45,6 +50,8 @@ public abstract class ScopedProxyUtils {
45
50
* @param registry the bean definition registry
46
51
* @param proxyTargetClass whether to create a target class proxy
47
52
* @return the scoped proxy definition
53
+ * @see #getTargetBeanName(String)
54
+ * @see #getOriginalBeanName(String)
48
55
*/
49
56
public static BeanDefinitionHolder createScopedProxy (BeanDefinitionHolder definition ,
50
57
BeanDefinitionRegistry registry , boolean proxyTargetClass ) {
@@ -93,11 +100,29 @@ public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder defini
93
100
* Generate the bean name that is used within the scoped proxy to reference the target bean.
94
101
* @param originalBeanName the original name of bean
95
102
* @return the generated bean to be used to reference the target bean
103
+ * @see #getOriginalBeanName(String)
96
104
*/
97
105
public static String getTargetBeanName (String originalBeanName ) {
98
106
return TARGET_NAME_PREFIX + originalBeanName ;
99
107
}
100
108
109
+ /**
110
+ * Get the original bean name for the provided {@linkplain #getTargetBeanName
111
+ * target bean name}.
112
+ * @param targetBeanName the target bean name for the scoped proxy
113
+ * @return the original bean name
114
+ * @throws IllegalArgumentException if the supplied bean name does not refer
115
+ * to the target of a scoped proxy
116
+ * @since 5.1.10
117
+ * @see #getTargetBeanName(String)
118
+ * @see #isScopedTarget(String)
119
+ */
120
+ public static String getOriginalBeanName (String targetBeanName ) {
121
+ Assert .isTrue (isScopedTarget (targetBeanName ), () -> "bean name '" +
122
+ targetBeanName + "' does not refer to the target of a scoped proxy" );
123
+ return targetBeanName .substring (TARGET_NAME_PREFIX_LENGTH );
124
+ }
125
+
101
126
/**
102
127
* Specify if the {@code beanName} is the name of a bean that references the target
103
128
* bean within a scoped proxy.
0 commit comments