Skip to content

Commit

Permalink
fix 并行调用多例Bean时 annotationWrapper 导致的线程安全问题
Browse files Browse the repository at this point in the history
  • Loading branch information
寻芳 committed Dec 24, 2024
1 parent 8c5adac commit 2d7d345
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,12 @@ private void processSofaReference(final Object bean, String beanName) {
return;
}

sofaReferenceAnnotation = annotationWrapper.wrap(sofaReferenceAnnotation);
if (annotationWrapper.get() == null) {
annotationWrapper.set(AnnotationWrapper.create(SofaReference.class)
.withEnvironment(applicationContext.getEnvironment())
.withBinder(DefaultPlaceHolderBinder.INSTANCE));

Check warning on line 103 in sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java#L101-L103

Added lines #L101 - L103 were not covered by tests
}
sofaReferenceAnnotation = annotationWrapper.get().wrap(sofaReferenceAnnotation);

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

sofaReferenceAnnotation = annotationWrapper.wrap(sofaReferenceAnnotation);
if (annotationWrapper.get() == null) {
annotationWrapper.set(AnnotationWrapper.create(SofaReference.class)
.withEnvironment(applicationContext.getEnvironment())
.withBinder(DefaultPlaceHolderBinder.INSTANCE));

Check warning on line 141 in sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java#L139-L141

Added lines #L139 - L141 were not covered by tests
}
sofaReferenceAnnotation = annotationWrapper.get().wrap(sofaReferenceAnnotation);

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

0 comments on commit 2d7d345

Please sign in to comment.