From e833dbaa37ab2fed624d7fe6f91553abe8fc1712 Mon Sep 17 00:00:00 2001 From: Matt Benson Date: Tue, 5 Jun 2018 23:02:55 -0500 Subject: [PATCH] Fix GenericScope so that multiple subclasses can live in the same context (non-static inner class cannot resolve parent bean) --- .../cloud/context/scope/GenericScope.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java index d0fa13bc..dfbcbcbb 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java @@ -256,6 +256,7 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, if (getName().equals(root.getDecoratedDefinition().getBeanDefinition() .getScope())) { root.setBeanClass(LockedScopedProxyFactoryBean.class); + root.getConstructorArgumentValues().addGenericArgumentValue(this); } } } @@ -311,6 +312,10 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, return this.name; } + protected ReadWriteLock getLock(String beanName) { + return locks.get(beanName); + } + private static class BeanLifecycleWrapperCache { private final ScopeCache cache; @@ -432,11 +437,16 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, } @SuppressWarnings("serial") - public class LockedScopedProxyFactoryBean extends ScopedProxyFactoryBean - implements MethodInterceptor { + public static class LockedScopedProxyFactoryBean + extends ScopedProxyFactoryBean implements MethodInterceptor { + private final S scope; private String targetBeanName; + public LockedScopedProxyFactoryBean(S scope) { + this.scope = scope; + } + @Override public void setBeanFactory(BeanFactory beanFactory) { super.setBeanFactory(beanFactory); @@ -463,7 +473,7 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, return invocation.proceed(); } Object proxy = getObject(); - Lock lock = locks.get(this.targetBeanName).readLock(); + Lock lock = scope.getLock(this.targetBeanName).readLock(); lock.lock(); try { if (proxy instanceof Advised) {