Skip to content

Commit 393cfcf

Browse files
jhoellerunknown
authored andcommitted
Cache InjectionMetadata per bean name instead of per Class
Issue: SPR-11027
1 parent 166b7a9 commit 393cfcf

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
121121
private final Map<Class<?>, Constructor<?>[]> candidateConstructorsCache =
122122
new ConcurrentHashMap<Class<?>, Constructor<?>[]>(64);
123123

124-
private final Map<Class<?>, InjectionMetadata> injectionMetadataCache =
125-
new ConcurrentHashMap<Class<?>, InjectionMetadata>(64);
124+
private final Map<String, InjectionMetadata> injectionMetadataCache =
125+
new ConcurrentHashMap<String, InjectionMetadata>(64);
126126

127127

128128
/**
@@ -217,7 +217,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
217217
@Override
218218
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
219219
if (beanType != null) {
220-
InjectionMetadata metadata = findAutowiringMetadata(beanType);
220+
InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType);
221221
metadata.checkConfigMembers(beanDefinition);
222222
}
223223
}
@@ -283,7 +283,7 @@ else if (candidate.getParameterTypes().length == 0) {
283283
public PropertyValues postProcessPropertyValues(
284284
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
285285

286-
InjectionMetadata metadata = findAutowiringMetadata(bean.getClass());
286+
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass());
287287
try {
288288
metadata.inject(bean, beanName, pvs);
289289
}
@@ -301,7 +301,7 @@ public PropertyValues postProcessPropertyValues(
301301
*/
302302
public void processInjection(Object bean) throws BeansException {
303303
Class<?> clazz = bean.getClass();
304-
InjectionMetadata metadata = findAutowiringMetadata(clazz);
304+
InjectionMetadata metadata = findAutowiringMetadata(clazz.getName(), clazz);
305305
try {
306306
metadata.inject(bean, null, null);
307307
}
@@ -311,15 +311,15 @@ public void processInjection(Object bean) throws BeansException {
311311
}
312312

313313

314-
private InjectionMetadata findAutowiringMetadata(Class<?> clazz) {
314+
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz) {
315315
// Quick check on the concurrent map first, with minimal locking.
316-
InjectionMetadata metadata = this.injectionMetadataCache.get(clazz);
316+
InjectionMetadata metadata = this.injectionMetadataCache.get(beanName);
317317
if (metadata == null) {
318318
synchronized (this.injectionMetadataCache) {
319-
metadata = this.injectionMetadataCache.get(clazz);
319+
metadata = this.injectionMetadataCache.get(beanName);
320320
if (metadata == null) {
321321
metadata = buildAutowiringMetadata(clazz);
322-
this.injectionMetadataCache.put(clazz, metadata);
322+
this.injectionMetadataCache.put(beanName, metadata);
323323
}
324324
}
325325
}

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
174174

175175
private transient BeanFactory beanFactory;
176176

177-
private transient final Map<Class<?>, InjectionMetadata> injectionMetadataCache =
178-
new ConcurrentHashMap<Class<?>, InjectionMetadata>(64);
177+
private transient final Map<String, InjectionMetadata> injectionMetadataCache =
178+
new ConcurrentHashMap<String, InjectionMetadata>(64);
179179

180180

181181
/**
@@ -280,7 +280,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
280280
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
281281
super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName);
282282
if (beanType != null) {
283-
InjectionMetadata metadata = findResourceMetadata(beanType);
283+
InjectionMetadata metadata = findResourceMetadata(beanName, beanType);
284284
metadata.checkConfigMembers(beanDefinition);
285285
}
286286
}
@@ -299,7 +299,7 @@ public boolean postProcessAfterInstantiation(Object bean, String beanName) throw
299299
public PropertyValues postProcessPropertyValues(
300300
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
301301

302-
InjectionMetadata metadata = findResourceMetadata(bean.getClass());
302+
InjectionMetadata metadata = findResourceMetadata(beanName, bean.getClass());
303303
try {
304304
metadata.inject(bean, beanName, pvs);
305305
}
@@ -310,12 +310,12 @@ public PropertyValues postProcessPropertyValues(
310310
}
311311

312312

313-
private InjectionMetadata findResourceMetadata(final Class<?> clazz) {
313+
private InjectionMetadata findResourceMetadata(String beanName, final Class<?> clazz) {
314314
// Quick check on the concurrent map first, with minimal locking.
315-
InjectionMetadata metadata = this.injectionMetadataCache.get(clazz);
315+
InjectionMetadata metadata = this.injectionMetadataCache.get(beanName);
316316
if (metadata == null) {
317317
synchronized (this.injectionMetadataCache) {
318-
metadata = this.injectionMetadataCache.get(clazz);
318+
metadata = this.injectionMetadataCache.get(beanName);
319319
if (metadata == null) {
320320
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
321321
Class<?> targetClass = clazz;
@@ -389,7 +389,7 @@ else if (method.isAnnotationPresent(Resource.class)) {
389389
while (targetClass != null && targetClass != Object.class);
390390

391391
metadata = new InjectionMetadata(clazz, elements);
392-
this.injectionMetadataCache.put(clazz, metadata);
392+
this.injectionMetadataCache.put(beanName, metadata);
393393
}
394394
}
395395
}

spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ public class PersistenceAnnotationBeanPostProcessor
187187

188188
private transient ListableBeanFactory beanFactory;
189189

190-
private transient final Map<Class<?>, InjectionMetadata> injectionMetadataCache =
191-
new ConcurrentHashMap<Class<?>, InjectionMetadata>(64);
190+
private transient final Map<String, InjectionMetadata> injectionMetadataCache =
191+
new ConcurrentHashMap<String, InjectionMetadata>(64);
192192

193193
private final Map<Object, EntityManager> extendedEntityManagersToClose =
194194
new ConcurrentHashMap<Object, EntityManager>(16);
@@ -328,7 +328,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
328328
@Override
329329
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
330330
if (beanType != null) {
331-
InjectionMetadata metadata = findPersistenceMetadata(beanType);
331+
InjectionMetadata metadata = findPersistenceMetadata(beanName, beanType);
332332
metadata.checkConfigMembers(beanDefinition);
333333
}
334334
}
@@ -347,7 +347,7 @@ public boolean postProcessAfterInstantiation(Object bean, String beanName) throw
347347
public PropertyValues postProcessPropertyValues(
348348
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
349349

350-
InjectionMetadata metadata = findPersistenceMetadata(bean.getClass());
350+
InjectionMetadata metadata = findPersistenceMetadata(beanName, bean.getClass());
351351
try {
352352
metadata.inject(bean, beanName, pvs);
353353
}
@@ -374,12 +374,12 @@ public void postProcessBeforeDestruction(Object bean, String beanName) throws Be
374374
}
375375

376376

377-
private InjectionMetadata findPersistenceMetadata(final Class<?> clazz) {
377+
private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?> clazz) {
378378
// Quick check on the concurrent map first, with minimal locking.
379-
InjectionMetadata metadata = this.injectionMetadataCache.get(clazz);
379+
InjectionMetadata metadata = this.injectionMetadataCache.get(beanName);
380380
if (metadata == null) {
381381
synchronized (this.injectionMetadataCache) {
382-
metadata = this.injectionMetadataCache.get(clazz);
382+
metadata = this.injectionMetadataCache.get(beanName);
383383
if (metadata == null) {
384384
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
385385
Class<?> targetClass = clazz;
@@ -417,7 +417,7 @@ private InjectionMetadata findPersistenceMetadata(final Class<?> clazz) {
417417
while (targetClass != null && targetClass != Object.class);
418418

419419
metadata = new InjectionMetadata(clazz, elements);
420-
this.injectionMetadataCache.put(clazz, metadata);
420+
this.injectionMetadataCache.put(beanName, metadata);
421421
}
422422
}
423423
}

spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525
import java.util.Properties;
26-
2726
import javax.persistence.EntityManager;
2827
import javax.persistence.EntityManagerFactory;
2928
import javax.persistence.PersistenceContext;
@@ -34,6 +33,7 @@
3433
import org.hibernate.ejb.HibernateEntityManager;
3534
import org.junit.Ignore;
3635
import org.junit.Test;
36+
3737
import org.springframework.beans.factory.FactoryBean;
3838
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3939
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -524,7 +524,7 @@ public void testSinglePersistenceContextFromJndi() {
524524
public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
525525
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
526526
try {
527-
babpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), null);
527+
babpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
528528
fail("Can't inject this field");
529529
}
530530
catch (IllegalStateException ex) {
@@ -536,7 +536,7 @@ public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
536536
public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
537537
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
538538
try {
539-
babpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), null);
539+
babpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
540540
fail("Can't inject this setter");
541541
}
542542
catch (IllegalStateException ex) {
@@ -548,7 +548,7 @@ public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
548548
public void testSetterWithNoArgs() {
549549
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
550550
try {
551-
babpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), null);
551+
babpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), "bean");
552552
fail("Can't inject this setter");
553553
}
554554
catch (IllegalStateException ex) {
@@ -593,7 +593,7 @@ public void testPropertiesForTransactionalEntityManager() {
593593
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
594594
DefaultPrivatePersistenceContextFieldWithProperties transactionalField =
595595
new DefaultPrivatePersistenceContextFieldWithProperties();
596-
babpp.postProcessPropertyValues(null, null, transactionalField, null);
596+
babpp.postProcessPropertyValues(null, null, transactionalField, "bean");
597597

598598
assertNotNull(transactionalField.em);
599599
assertNotNull(transactionalField.em.getDelegate());
@@ -620,8 +620,8 @@ public void testPropertiesForSharedEntityManager1() {
620620
new DefaultPrivatePersistenceContextFieldWithProperties();
621621
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
622622

623-
babpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, null);
624-
babpp.postProcessPropertyValues(null, null, transactionalField, null);
623+
babpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
624+
babpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
625625

626626
assertNotNull(transactionalFieldWithProperties.em);
627627
assertNotNull(transactionalField.em);
@@ -653,8 +653,8 @@ public void testPropertiesForSharedEntityManager2() {
653653
new DefaultPrivatePersistenceContextFieldWithProperties();
654654
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
655655

656-
babpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, null);
657-
babpp.postProcessPropertyValues(null, null, transactionalField, null);
656+
babpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
657+
babpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
658658

659659
assertNotNull(transactionalFieldWithProperties.em);
660660
assertNotNull(transactionalField.em);

0 commit comments

Comments
 (0)