New postProcessProperties variant on InstantiationAwareBeanPostProcessor

Allows for skipping the now-deprecated postProcessPropertyValues callback with its expensive PropertyDescriptor retrieval requirement. RequiredAnnotationBeanPostProcessor (which is dependent on postProcessPropertyValues) and the @Required annotation itself are also deprecated now: in favor of constructor injection (or afterPropertiesSet).

Issue: SPR-16918
This commit is contained in:
Juergen Hoeller
2018-06-28 13:29:08 +02:00
parent 794693525f
commit 81cb740e0a
19 changed files with 178 additions and 123 deletions

View File

@@ -35,7 +35,6 @@ import javax.persistence.PersistenceUnit;
import javax.persistence.SynchronizationType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
@@ -334,19 +333,17 @@ public class PersistenceAnnotationBeanPostProcessor
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
public boolean postProcessAfterInstantiation(Object bean, String beanName) {
return true;
}
@Override
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
InjectionMetadata metadata = findPersistenceMetadata(beanName, bean.getClass(), pvs);
try {
metadata.inject(bean, beanName, pvs);
@@ -357,18 +354,26 @@ public class PersistenceAnnotationBeanPostProcessor
return pvs;
}
@Deprecated
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
return postProcessProperties(pvs, bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
public void postProcessBeforeDestruction(Object bean, String beanName) {
EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
EntityManagerFactoryUtils.closeEntityManager(emToClose);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -54,7 +54,7 @@ public class PersistenceContextTransactionTests {
@Before
public void setUp() throws Exception {
public void setup() {
factory = mock(EntityManagerFactory.class);
manager = mock(EntityManager.class);
tx = mock(EntityTransaction.class);
@@ -74,14 +74,14 @@ public class PersistenceContextTransactionTests {
return factory;
}
};
pabpp.postProcessPropertyValues(null, null, bean, "bean");
pabpp.postProcessProperties(null, bean, "bean");
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
@After
public void tearDown() throws Exception {
public void clear() {
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

View File

@@ -546,7 +546,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
pabpp.postProcessPropertyValues(null, null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
pabpp.postProcessProperties(null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
fail("Can't inject this field");
}
catch (IllegalStateException ex) {
@@ -558,7 +558,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
pabpp.postProcessPropertyValues(null, null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
pabpp.postProcessProperties(null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
fail("Can't inject this setter");
}
catch (IllegalStateException ex) {
@@ -570,7 +570,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
public void testSetterWithNoArgs() {
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
pabpp.postProcessPropertyValues(null, null, new SetterWithNoArgs(), "bean");
pabpp.postProcessProperties(null, new SetterWithNoArgs(), "bean");
fail("Can't inject this setter");
}
catch (IllegalStateException ex) {
@@ -585,7 +585,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldExtended dppcf = new DefaultPrivatePersistenceContextFieldExtended();
pabpp.postProcessPropertyValues(null, null, dppcf, "bean");
pabpp.postProcessProperties(null, dppcf, "bean");
assertNotNull(dppcf.em);
}
@@ -599,7 +599,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldExtendedWithProps dppcf =
new DefaultPrivatePersistenceContextFieldExtendedWithProps();
pabpp.postProcessPropertyValues(null, null, dppcf, "bean");
pabpp.postProcessProperties(null, dppcf, "bean");
assertNotNull(dppcf.em);
}
@@ -615,7 +615,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
DefaultPrivatePersistenceContextFieldWithProperties transactionalField =
new DefaultPrivatePersistenceContextFieldWithProperties();
pabpp.postProcessPropertyValues(null, null, transactionalField, "bean");
pabpp.postProcessProperties(null, transactionalField, "bean");
assertNotNull(transactionalField.em);
assertNotNull(transactionalField.em.getDelegate());
@@ -642,8 +642,8 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
new DefaultPrivatePersistenceContextFieldWithProperties();
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
pabpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessProperties(null, transactionalField, "bean2");
assertNotNull(transactionalFieldWithProperties.em);
assertNotNull(transactionalField.em);
@@ -675,8 +675,8 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
new DefaultPrivatePersistenceContextFieldWithProperties();
DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();
pabpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessPropertyValues(null, null, transactionalField, "bean2");
pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
pabpp.postProcessProperties(null, transactionalField, "bean2");
assertNotNull(transactionalFieldWithProperties.em);
assertNotNull(transactionalField.em);