diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index 8478cf6f2f..23995bcac9 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -35,6 +35,7 @@ import java.util.LinkedList; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; + import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; @@ -509,9 +510,12 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean */ private class ResourceElement extends LookupElement { - @SuppressWarnings("unused") protected boolean shareable = true; + private volatile boolean cached = false; + + private volatile Object cachedFieldValue; + public ResourceElement(Member member, PropertyDescriptor pd) { super(member, pd); } @@ -546,7 +550,20 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean @Override protected Object getResourceToInject(Object target, String requestingBeanName) { - return getResource(this, requestingBeanName); + Object value = null; + if (this.cached && this.shareable) { + value = this.cachedFieldValue; + } + synchronized (this) { + if (!this.cached) { + value = getResource(this, requestingBeanName); + if (value != null && this.shareable) { + this.cachedFieldValue = value; + this.cached = true; + } + } + } + return value; } } @@ -724,4 +741,4 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean } } -} +} \ No newline at end of file