Skip to content

Commit 592e344

Browse files
committed
PersistenceAnnotationBeanPostProcessor defensively handles BeanDefinition access for extended EntityManagers
Issue: SPR-8834
1 parent 9d3d6d5 commit 592e344

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
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.
@@ -713,8 +713,8 @@ private EntityManager resolveExtendedEntityManager(Object target, String request
713713
em = ExtendedEntityManagerCreator.createContainerManagedEntityManager(
714714
emf, this.properties, this.synchronizedWithTransaction);
715715
}
716-
if (em instanceof EntityManagerProxy &&
717-
beanFactory != null && !beanFactory.isPrototype(requestingBeanName)) {
716+
if (em instanceof EntityManagerProxy && beanFactory != null &&
717+
beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) {
718718
extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager());
719719
}
720720
return em;

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

+21-1
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.
@@ -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();

0 commit comments

Comments
 (0)