Skip to content

Commit 388561d

Browse files
committed
PersistenceAnnotationBeanPostProcessor defensively handles BeanDefinition access for extended EntityManagers
Issue: SPR-8834 (cherry picked from commit 592e344)
1 parent e89f18b commit 388561d

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -691,8 +691,8 @@ private EntityManager resolveExtendedEntityManager(Object target, String request
691691
// Inject a container-managed extended EntityManager.
692692
em = ExtendedEntityManagerCreator.createContainerManagedEntityManager(emf, this.properties);
693693
}
694-
if (em instanceof EntityManagerProxy &&
695-
beanFactory != null && !beanFactory.isPrototype(requestingBeanName)) {
694+
if (em instanceof EntityManagerProxy && beanFactory != null &&
695+
beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) {
696696
extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
697697
}
698698
return em;

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

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -140,6 +140,26 @@ public void testPublicSpecificExtendedPersistenceContextSetter() throws Exceptio
140140
verify(mockEm2).flush();
141141
}
142142

143+
@Test
144+
public void testInjectionIntoExistingObjects() {
145+
EntityManager mockEm = mock(EntityManager.class);
146+
given(mockEmf.createEntityManager()).willReturn(mockEm);
147+
148+
GenericApplicationContext gac = new GenericApplicationContext();
149+
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
150+
gac.registerBeanDefinition("annotationProcessor",
151+
new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
152+
gac.refresh();
153+
154+
DefaultPrivatePersistenceContextField existingBean1 = new DefaultPrivatePersistenceContextField();
155+
gac.getAutowireCapableBeanFactory().autowireBean(existingBean1);
156+
assertNotNull(existingBean1.em);
157+
158+
DefaultPublicPersistenceContextSetter existingBean2 = new DefaultPublicPersistenceContextSetter();
159+
gac.getAutowireCapableBeanFactory().autowireBean(existingBean2);
160+
assertNotNull(existingBean2.em);
161+
}
162+
143163
@Test
144164
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
145165
DummyInvocationHandler ih = new DummyInvocationHandler();
@@ -524,7 +544,7 @@ public void testSinglePersistenceContextFromJndi() {
524544
public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
525545
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
526546
try {
527-
babpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), null);
547+
babpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
528548
fail("Can't inject this field");
529549
}
530550
catch (IllegalStateException ex) {
@@ -536,7 +556,7 @@ public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
536556
public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
537557
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
538558
try {
539-
babpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), null);
559+
babpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
540560
fail("Can't inject this setter");
541561
}
542562
catch (IllegalStateException ex) {
@@ -548,7 +568,7 @@ public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
548568
public void testSetterWithNoArgs() {
549569
PersistenceAnnotationBeanPostProcessor babpp = new PersistenceAnnotationBeanPostProcessor();
550570
try {
551-
babpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), null);
571+
babpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), "bean");
552572
fail("Can't inject this setter");
553573
}
554574
catch (IllegalStateException ex) {
@@ -593,7 +613,7 @@ public void testPropertiesForTransactionalEntityManager() {
593613
PersistenceAnnotationBeanPostProcessor babpp = new MockPersistenceAnnotationBeanPostProcessor();
594614
DefaultPrivatePersistenceContextFieldWithProperties transactionalField =
595615
new DefaultPrivatePersistenceContextFieldWithProperties();
596-
babpp.postProcessPropertyValues(null, null, transactionalField, null);
616+
babpp.postProcessPropertyValues(null, null, transactionalField, "bean");
597617

598618
assertNotNull(transactionalField.em);
599619
assertNotNull(transactionalField.em.getDelegate());
@@ -620,8 +640,8 @@ public void testPropertiesForSharedEntityManager1() {
620640
new DefaultPrivatePersistenceContextFieldWithProperties();
621641
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
622642

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

626646
assertNotNull(transactionalFieldWithProperties.em);
627647
assertNotNull(transactionalField.em);
@@ -653,8 +673,8 @@ public void testPropertiesForSharedEntityManager2() {
653673
new DefaultPrivatePersistenceContextFieldWithProperties();
654674
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
655675

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

659679
assertNotNull(transactionalFieldWithProperties.em);
660680
assertNotNull(transactionalField.em);

0 commit comments

Comments
 (0)