Avoid hard-coded SCOPED_TARGET_PREFIX usage. (#811)

Co-authored-by: Spencer Gibb <sgibb@pivotal.io>
This commit is contained in:
Ian Hu
2021-02-18 00:34:59 +08:00
committed by GitHub
parent 36d5350661
commit e42f0987bb
8 changed files with 18 additions and 16 deletions

View File

@@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,7 +34,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.integration.RefreshScopeIntegrationTests.TestConfiguration;
import org.springframework.cloud.context.scope.GenericScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
@@ -117,7 +117,7 @@ public class RefreshScopeIntegrationTests {
then(ExampleService.getDestroyCount()).isEqualTo(1);
then(id2).isNotSameAs(id1);
then(ExampleService.event).isNotNull();
then(ExampleService.event.getName()).isEqualTo(GenericScope.SCOPED_TARGET_PREFIX + "service");
then(ExampleService.event.getName()).isEqualTo(ScopedProxyUtils.getTargetBeanName("service"));
}
// see gh-349

View File

@@ -73,11 +73,6 @@ import org.springframework.util.StringUtils;
public class GenericScope
implements Scope, BeanFactoryPostProcessor, BeanDefinitionRegistryPostProcessor, DisposableBean {
/**
* Prefix for the scoped target.
*/
public static final String SCOPED_TARGET_PREFIX = "scopedTarget.";
private static final Log logger = LogFactory.getLog(GenericScope.class);
private BeanLifecycleWrapperCache cache = new BeanLifecycleWrapperCache(new StandardScopeCache());

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.context.scope.refresh;
import java.io.Serializable;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -132,10 +133,10 @@ public class RefreshScope extends GenericScope
@ManagedOperation(description = "Dispose of the current instance of bean name "
+ "provided and force a refresh on next method execution.")
public boolean refresh(String name) {
if (!name.startsWith(SCOPED_TARGET_PREFIX)) {
if (!ScopedProxyUtils.isScopedTarget(name)) {
// User wants to refresh the bean with this name but that isn't the one in the
// cache...
name = SCOPED_TARGET_PREFIX + name;
name = ScopedProxyUtils.getTargetBeanName(name);
}
// Ensure lifecycle is finished if bean was disposable
if (super.destroy(name)) {

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.context.scope.refresh;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.test.context.SpringBootTest;
@@ -47,7 +48,8 @@ public class ImportRefreshScopeIntegrationTests {
@Test
public void testSimpleProperties() throws Exception {
then(this.service.getMessage()).isEqualTo("Hello scope!");
then(this.beanFactory.getBeanDefinition("scopedTarget.service").getScope()).isEqualTo("refresh");
then(this.beanFactory.getBeanDefinition(ScopedProxyUtils.getTargetBeanName("service")).getScope())
.isEqualTo("refresh");
then(this.service.getMessage()).isEqualTo("Hello scope!");
}

View File

@@ -21,6 +21,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -72,7 +73,8 @@ public class RefreshScopeConfigurationTests {
PropertyPlaceholderAutoConfiguration.class, RefreshAutoConfiguration.class,
LifecycleMvcEndpointAutoConfiguration.class);
Application application = this.context.getBean(Application.class);
then(this.context.getBeanDefinition("scopedTarget.application").getScope()).isEqualTo("refresh");
then(this.context.getBeanDefinition(ScopedProxyUtils.getTargetBeanName("application")).getScope())
.isEqualTo("refresh");
application.hello();
refresh();
String message = application.hello();

View File

@@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,7 +33,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.GenericScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeIntegrationTests.TestConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
@@ -115,7 +115,7 @@ public class RefreshScopeIntegrationTests {
then(ExampleService.getDestroyCount()).isEqualTo(1);
then(id2).isNotSameAs(id1);
then(ExampleService.event).isNotNull();
then(ExampleService.event.getName()).isEqualTo(GenericScope.SCOPED_TARGET_PREFIX + "service");
then(ExampleService.event.getName()).isEqualTo(ScopedProxyUtils.getTargetBeanName("service"));
}
// see gh-349

View File

@@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,7 +35,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.GenericScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeLazyIntegrationTests.TestConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
@@ -117,7 +117,7 @@ public class RefreshScopeLazyIntegrationTests {
then(ExampleService.getDestroyCount()).isEqualTo(1);
then(id2).isNotSameAs(id1);
then(ExampleService.event).isNotNull();
then(ExampleService.event.getName()).isEqualTo(GenericScope.SCOPED_TARGET_PREFIX + "service");
then(ExampleService.event.getName()).isEqualTo(ScopedProxyUtils.getTargetBeanName("service"));
}
public interface Service {

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.context.scope.refresh;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -58,7 +59,8 @@ public class RefreshScopeWebIntegrationTests {
@Test
public void scopeOnBeanDefinition() throws Exception {
then(this.beanFactory.getBeanDefinition("scopedTarget.application").getScope()).isEqualTo("refresh");
then(this.beanFactory.getBeanDefinition(ScopedProxyUtils.getTargetBeanName("application")).getScope())
.isEqualTo("refresh");
}
@Test