Skip to content

Commit

Permalink
fix 并行调用多例Bean时 annotationWrapper 导致的线程安全问题 (#1354)
Browse files Browse the repository at this point in the history
* fix 并行调用多例Bean时 annotationWrapper 导致的线程安全问题

* fix 并行调用多例Bean时 annotationWrapper 导致的线程安全问题

* format code

---------

Co-authored-by: 寻芳 <dengjialin.djl@antgroup.com>
  • Loading branch information
crazysaltfish and 寻芳 authored Dec 24, 2024
1 parent 8c5adac commit cf9de07
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@
public class ReferenceAnnotationBeanPostProcessor implements BeanPostProcessor,
ApplicationContextAware, PriorityOrdered {

private static final Logger LOGGER = SofaBootLoggerFactory
.getLogger(ReferenceAnnotationBeanPostProcessor.class);
private static final Logger LOGGER = SofaBootLoggerFactory
.getLogger(ReferenceAnnotationBeanPostProcessor.class);

private final SofaRuntimeContext sofaRuntimeContext;
private final SofaRuntimeContext sofaRuntimeContext;

private final BindingAdapterFactory bindingAdapterFactory;
private final BindingAdapterFactory bindingAdapterFactory;

private final BindingConverterFactory bindingConverterFactory;
private final BindingConverterFactory bindingConverterFactory;

private ApplicationContext applicationContext;
private ApplicationContext applicationContext;

private AnnotationWrapper<SofaReference> annotationWrapper;
private final ThreadLocal<AnnotationWrapper<SofaReference>> annotationWrapper = new ThreadLocal<>();

/**
* To construct a ReferenceAnnotationBeanPostProcessor via a Spring Bean
Expand Down Expand Up @@ -97,7 +97,10 @@ private void processSofaReference(final Object bean, String beanName) {
return;
}

sofaReferenceAnnotation = annotationWrapper.wrap(sofaReferenceAnnotation);
if (annotationWrapper.get() == null) {
annotationWrapper.set(createAnnotationWrapper());
}
sofaReferenceAnnotation = annotationWrapper.get().wrap(sofaReferenceAnnotation);

Class<?> interfaceType = sofaReferenceAnnotation.interfaceType();
if (interfaceType.equals(void.class)) {
Expand Down Expand Up @@ -130,7 +133,10 @@ private void processSofaReference(final Object bean, String beanName) {
return;
}

sofaReferenceAnnotation = annotationWrapper.wrap(sofaReferenceAnnotation);
if (annotationWrapper.get() == null) {
annotationWrapper.set(createAnnotationWrapper());
}
sofaReferenceAnnotation = annotationWrapper.get().wrap(sofaReferenceAnnotation);

Class<?> interfaceType = sofaReferenceAnnotation.interfaceType();
if (interfaceType.equals(void.class)) {
Expand Down Expand Up @@ -182,8 +188,12 @@ public int getOrder() {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
this.annotationWrapper = AnnotationWrapper.create(SofaReference.class)
.withEnvironment(applicationContext.getEnvironment())
this.annotationWrapper.set(createAnnotationWrapper());
}

private AnnotationWrapper<SofaReference> createAnnotationWrapper() {
return AnnotationWrapper.create(SofaReference.class)
.withEnvironment(this.applicationContext.getEnvironment())
.withBinder(DefaultPlaceHolderBinder.INSTANCE);
}
}

0 comments on commit cf9de07

Please sign in to comment.