Fixed premature initialization of AuditorAware implementations.

Set bean scope of AuditingEntityListener to prototype to ensure DI for each instance. Before that AuditorAware implementations referencing a Repository would cause premature initialization of those as the AuditingEntityListener gets created and thus wired when the EntityManagerFactory gets created. At that point in time we unfortunately don't have no DAOs yet.

Added @DirtiesContext to AuditingEntitListenerIntegrationTest because otherwise the @Configurable backing prototype bean definition leaks into other test cases.
This commit is contained in:
Oliver Gierke
2011-01-08 09:27:16 +01:00
parent b6767afadc
commit 3db9240647
9 changed files with 66 additions and 12 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.data.jpa.domain.sample;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.sample.AuditableUserRepository;
import org.springframework.util.Assert;
/**
@@ -26,9 +28,18 @@ import org.springframework.data.domain.AuditorAware;
*/
public class AuditorAwareStub implements AuditorAware<AuditableUser> {
@SuppressWarnings("unused")
private final AuditableUserRepository repository;
private AuditableUser auditor;
public AuditorAwareStub(AuditableUserRepository repository) {
Assert.notNull(repository);
this.repository = repository;
}
public void setAuditor(AuditableUser auditor) {
this.auditor = auditor;

View File

@@ -27,6 +27,7 @@ import org.springframework.data.jpa.domain.sample.AuditableRole;
import org.springframework.data.jpa.domain.sample.AuditableUser;
import org.springframework.data.jpa.domain.sample.AuditorAwareStub;
import org.springframework.data.jpa.repository.sample.AuditableUserRepository;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -40,6 +41,7 @@ import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:auditing/auditing-entity-listener.xml")
@Transactional
@DirtiesContext
public class AuditingEntityListenerTests {
@Autowired

View File

@@ -44,7 +44,7 @@ public class AuditingEntityListenerUnitTests {
listener = new AuditingEntityListener<AuditableUser>();
// Explicitly null the AuditorAware as it might have been DI'ed if test
// is run in a test suite with integration tests
listener.setAuditorAware(null);
// listener.setAuditorAware(null);
user = new AuditableUser();
@@ -65,7 +65,6 @@ public class AuditingEntityListenerUnitTests {
assertNotNull(user.getCreatedDate());
assertNotNull(user.getLastModifiedDate());
System.out.println(user.getCreatedBy());
assertNull(user.getCreatedBy());
assertNull(user.getLastModifiedBy());
}

View File

@@ -15,11 +15,12 @@
*/
package org.springframework.data.jpa.domain.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
/**
@@ -49,8 +50,8 @@ public class AuditingNamespaceUnitTests extends
BeanDefinition definition =
beanFactory.getBeanDefinition(AuditingEntityListener.class
.getName());
assertEquals(
definition.getPropertyValues().getPropertyValue("auditorAware")
.getValue(), new RuntimeBeanReference("auditorAware"));
PropertyValue propertyValue =
definition.getPropertyValues().getPropertyValue("auditorAware");
assertThat(propertyValue, is(notNullValue()));
}
}