Skip to content

Commit 9d2a874

Browse files
committed
Introduce getOriginalBeanName(String) in ScopedProxyUtils
This commit introduces a utility method for retrieving the original bean name for the target of a scoped proxy. Closes gh-23514
1 parent a7bb5ca commit 9d2a874

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,20 +23,25 @@
2323
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2424
import org.springframework.beans.factory.support.RootBeanDefinition;
2525
import org.springframework.lang.Nullable;
26+
import org.springframework.util.Assert;
2627

2728
/**
2829
* Utility class for creating a scoped proxy.
29-
* Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner.
30+
*
31+
* <p>Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner.
3032
*
3133
* @author Mark Fisher
3234
* @author Juergen Hoeller
3335
* @author Rob Harrop
36+
* @author Sam Brannen
3437
* @since 2.5
3538
*/
3639
public abstract class ScopedProxyUtils {
3740

3841
private static final String TARGET_NAME_PREFIX = "scopedTarget.";
3942

43+
private static final int TARGET_NAME_PREFIX_LENGTH = TARGET_NAME_PREFIX.length();
44+
4045

4146
/**
4247
* Generate a scoped proxy for the supplied target bean, registering the target
@@ -45,6 +50,8 @@ public abstract class ScopedProxyUtils {
4550
* @param registry the bean definition registry
4651
* @param proxyTargetClass whether to create a target class proxy
4752
* @return the scoped proxy definition
53+
* @see #getTargetBeanName(String)
54+
* @see #getOriginalBeanName(String)
4855
*/
4956
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition,
5057
BeanDefinitionRegistry registry, boolean proxyTargetClass) {
@@ -93,11 +100,29 @@ public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder defini
93100
* Generate the bean name that is used within the scoped proxy to reference the target bean.
94101
* @param originalBeanName the original name of bean
95102
* @return the generated bean to be used to reference the target bean
103+
* @see #getOriginalBeanName(String)
96104
*/
97105
public static String getTargetBeanName(String originalBeanName) {
98106
return TARGET_NAME_PREFIX + originalBeanName;
99107
}
100108

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+
101126
/**
102127
* Specify if the {@code beanName} is the name of a bean that references the target
103128
* bean within a scoped proxy.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2002-2019 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+
* https://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.scope;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.rules.ExpectedException;
22+
23+
import static org.junit.Assert.*;
24+
25+
/**
26+
* Unit tests for {@link ScopedProxyUtils}.
27+
*
28+
* @author Sam Brannen
29+
* @since 5.1.10
30+
*/
31+
public class ScopedProxyUtilsTests {
32+
33+
@Rule
34+
public final ExpectedException exception = ExpectedException.none();
35+
36+
37+
@Test
38+
public void getTargetBeanNameAndIsScopedTarget() {
39+
String originalBeanName = "myBean";
40+
String targetBeanName = ScopedProxyUtils.getTargetBeanName(originalBeanName);
41+
42+
assertNotEquals(originalBeanName, targetBeanName);
43+
assertTrue(targetBeanName.endsWith(originalBeanName));
44+
assertTrue(ScopedProxyUtils.isScopedTarget(targetBeanName));
45+
assertFalse(ScopedProxyUtils.isScopedTarget(originalBeanName));
46+
}
47+
48+
@Test
49+
public void getOriginalBeanNameAndIsScopedTarget() {
50+
String originalBeanName = "myBean";
51+
String targetBeanName = ScopedProxyUtils.getTargetBeanName(originalBeanName);
52+
String parsedOriginalBeanName = ScopedProxyUtils.getOriginalBeanName(targetBeanName);
53+
54+
assertNotEquals(targetBeanName, parsedOriginalBeanName);
55+
assertEquals(originalBeanName, parsedOriginalBeanName);
56+
assertTrue(ScopedProxyUtils.isScopedTarget(targetBeanName));
57+
assertFalse(ScopedProxyUtils.isScopedTarget(parsedOriginalBeanName));
58+
}
59+
60+
@Test
61+
public void getOriginalBeanNameForNullTargetBean() {
62+
exception.expect(IllegalArgumentException.class);
63+
exception.expectMessage("bean name 'null' does not refer to the target of a scoped proxy");
64+
ScopedProxyUtils.getOriginalBeanName(null);
65+
}
66+
67+
@Test
68+
public void getOriginalBeanNameForNonScopedTarget() {
69+
exception.expect(IllegalArgumentException.class);
70+
exception.expectMessage("bean name 'myBean' does not refer to the target of a scoped proxy");
71+
ScopedProxyUtils.getOriginalBeanName("myBean");
72+
}
73+
74+
}

0 commit comments

Comments
 (0)