提交 9be6aa64 编写于 作者: S Sam Brannen

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
上级 dda04762
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,20 +23,25 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; ...@@ -23,20 +23,25 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/** /**
* Utility class for creating a scoped proxy. * Utility class for creating a scoped proxy.
* Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner. *
* <p>Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner.
* *
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
* @author Sam Brannen
* @since 2.5 * @since 2.5
*/ */
public abstract class ScopedProxyUtils { public abstract class ScopedProxyUtils {
private static final String TARGET_NAME_PREFIX = "scopedTarget."; private static final String TARGET_NAME_PREFIX = "scopedTarget.";
private static final int TARGET_NAME_PREFIX_LENGTH = TARGET_NAME_PREFIX.length();
/** /**
* Generate a scoped proxy for the supplied target bean, registering the target * Generate a scoped proxy for the supplied target bean, registering the target
...@@ -45,6 +50,8 @@ public abstract class ScopedProxyUtils { ...@@ -45,6 +50,8 @@ public abstract class ScopedProxyUtils {
* @param registry the bean definition registry * @param registry the bean definition registry
* @param proxyTargetClass whether to create a target class proxy * @param proxyTargetClass whether to create a target class proxy
* @return the scoped proxy definition * @return the scoped proxy definition
* @see #getTargetBeanName(String)
* @see #getOriginalBeanName(String)
*/ */
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition, public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition,
BeanDefinitionRegistry registry, boolean proxyTargetClass) { BeanDefinitionRegistry registry, boolean proxyTargetClass) {
...@@ -93,11 +100,29 @@ public abstract class ScopedProxyUtils { ...@@ -93,11 +100,29 @@ public abstract class ScopedProxyUtils {
* Generate the bean name that is used within the scoped proxy to reference the target bean. * Generate the bean name that is used within the scoped proxy to reference the target bean.
* @param originalBeanName the original name of bean * @param originalBeanName the original name of bean
* @return the generated bean to be used to reference the target bean * @return the generated bean to be used to reference the target bean
* @see #getOriginalBeanName(String)
*/ */
public static String getTargetBeanName(String originalBeanName) { public static String getTargetBeanName(String originalBeanName) {
return TARGET_NAME_PREFIX + originalBeanName; return TARGET_NAME_PREFIX + originalBeanName;
} }
/**
* Get the original bean name for the provided {@linkplain #getTargetBeanName
* target bean name}.
* @param targetBeanName the target bean name for the scoped proxy
* @return the original bean name
* @throws IllegalArgumentException if the supplied bean name does not refer
* to the target of a scoped proxy
* @since 5.2
* @see #getTargetBeanName(String)
* @see #isScopedTarget(String)
*/
public static String getOriginalBeanName(String targetBeanName) {
Assert.isTrue(isScopedTarget(targetBeanName), () -> "bean name '" +
targetBeanName + "' does not refer to the target of a scoped proxy");
return targetBeanName.substring(TARGET_NAME_PREFIX_LENGTH);
}
/** /**
* Specify if the {@code beanName} is the name of a bean that references the target * Specify if the {@code beanName} is the name of a bean that references the target
* bean within a scoped proxy. * bean within a scoped proxy.
......
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.scope;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Unit tests for {@link ScopedProxyUtils}.
*
* @author Sam Brannen
* @since 5.2
*/
class ScopedProxyUtilsTests {
@Test
void getTargetBeanNameAndIsScopedTarget() {
String originalBeanName = "myBean";
String targetBeanName = ScopedProxyUtils.getTargetBeanName(originalBeanName);
assertThat(targetBeanName).isNotEqualTo(originalBeanName).endsWith(originalBeanName);
assertThat(ScopedProxyUtils.isScopedTarget(targetBeanName)).isTrue();
assertThat(ScopedProxyUtils.isScopedTarget(originalBeanName)).isFalse();
}
@Test
void getOriginalBeanNameAndIsScopedTarget() {
String originalBeanName = "myBean";
String targetBeanName = ScopedProxyUtils.getTargetBeanName(originalBeanName);
String parsedOriginalBeanName = ScopedProxyUtils.getOriginalBeanName(targetBeanName);
assertThat(parsedOriginalBeanName).isNotEqualTo(targetBeanName).isEqualTo(originalBeanName);
assertThat(ScopedProxyUtils.isScopedTarget(targetBeanName)).isTrue();
assertThat(ScopedProxyUtils.isScopedTarget(parsedOriginalBeanName)).isFalse();
}
@Test
void getOriginalBeanNameForNullTargetBean() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ScopedProxyUtils.getOriginalBeanName(null))
.withMessage("bean name 'null' does not refer to the target of a scoped proxy");
}
@Test
void getOriginalBeanNameForNonScopedTarget() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ScopedProxyUtils.getOriginalBeanName("myBean"))
.withMessage("bean name 'myBean' does not refer to the target of a scoped proxy");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册