diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java index 4ddf79cb5c..e212b36bae 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java @@ -16,7 +16,6 @@ package org.springframework.aop.framework.autoproxy; -import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Arrays; @@ -230,12 +229,12 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport @Override @Nullable - public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException { + public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) { return null; } @Override - public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException { + public Object getEarlyBeanReference(Object bean, String beanName) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (!this.earlyProxyReferences.contains(cacheKey)) { this.earlyProxyReferences.add(cacheKey); @@ -244,7 +243,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport } @Override - public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { + public Object postProcessBeforeInstantiation(Class beanClass, String beanName) { Object cacheKey = getCacheKey(beanClass, beanName); if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) { @@ -280,9 +279,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport } @Override - public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { - + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { return pvs; } @@ -297,7 +294,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport * @see #getAdvicesAndAdvisorsForBean */ @Override - public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) throws BeansException { + public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (!this.earlyProxyReferences.contains(cacheKey)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 5216c2645f..d052f4ed85 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -365,9 +365,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean } @Override - public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException { - + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs); try { metadata.inject(bean, beanName, pvs); @@ -381,6 +379,14 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean return pvs; } + @Deprecated + @Override + public PropertyValues postProcessPropertyValues( + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { + + return postProcessProperties(pvs, bean, beanName); + } + /** * 'Native' processing method for direct calls with an arbitrary target instance, * resolving all of its fields and methods which are annotated with {@code @Autowired}. diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Required.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Required.java index bb342c7854..915543a588 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Required.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Required.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -31,7 +31,10 @@ import java.lang.annotation.Target; * @author Rob Harrop * @since 2.0 * @see RequiredAnnotationBeanPostProcessor + * @deprecated as of 5.1, in favor of using constructor injection for required settings + * (or a custom {@link org.springframework.beans.factory.InitializingBean} implementation) */ +@Deprecated @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Required { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java index 7637f5460d..b1a9be2d33 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; @@ -72,7 +71,10 @@ import org.springframework.util.Assert; * @since 2.0 * @see #setRequiredAnnotationType * @see Required + * @deprecated as of 5.1, in favor of using constructor injection for required settings + * (or a custom {@link org.springframework.beans.factory.InitializingBean} implementation) */ +@Deprecated public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware { @@ -142,7 +144,7 @@ public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanP @Override public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { if (!this.validatedBeanNames.contains(beanName)) { if (!shouldSkip(this.beanFactory, beanName)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java index 15b162677a..525ecebea5 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -55,7 +55,7 @@ public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { * {@link #postProcessAfterInitialization} callback from the configured * {@link BeanPostProcessor BeanPostProcessors}. *

This callback will only be applied to bean definitions with a bean class. - * In particular, it will not be applied to beans with a "factory-method". + * In particular, it will not be applied to beans with a factory method. *

Post-processors may implement the extended * {@link SmartInstantiationAwareBeanPostProcessor} interface in order * to predict the type of the bean object that they are going to return here. @@ -65,8 +65,8 @@ public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { * @return the bean object to expose instead of a default instance of the target bean, * or {@code null} to proceed with default instantiation * @throws org.springframework.beans.BeansException in case of errors + * @see #postProcessAfterInstantiation * @see org.springframework.beans.factory.support.AbstractBeanDefinition#hasBeanClass - * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getFactoryMethodName */ @Nullable default Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { @@ -86,11 +86,37 @@ public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { * Returning {@code false} will also prevent any subsequent InstantiationAwareBeanPostProcessor * instances being invoked on this bean instance. * @throws org.springframework.beans.BeansException in case of errors + * @see #postProcessBeforeInstantiation */ default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { return true; } + /** + * Post-process the given property values before the factory applies them + * to the given bean, without any need for property descriptors. + *

Implementations should return {@code null} (the default) if they provide a custom + * {@link #postProcessPropertyValues} implementation, and {@code pvs} otherwise. + * In a future version of this interface (with {@link #postProcessPropertyValues} removed), + * the default implementation will return the given {@code pvs} as-is directly. + * @param pvs the property values that the factory is about to apply (never {@code null}) + * @param bean the bean instance created, but whose properties have not yet been set + * @param beanName the name of the bean + * @return the actual property values to apply to the given bean (can be the passed-in + * PropertyValues instance), or {@code null} which proceeds with the existing properties + * but specifically continues with a call to {@link #postProcessPropertyValues} + * (requiring initialized {@code PropertyDescriptor}s for the current bean class) + * @throws org.springframework.beans.BeansException in case of errors + * @since 5.1 + * @see #postProcessPropertyValues + */ + @Nullable + default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) + throws BeansException { + + return null; + } + /** * Post-process the given property values before the factory applies them * to the given bean. Allows for checking whether all dependencies have been @@ -104,12 +130,14 @@ public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { * dependency types - which the factory handles specifically - already filtered out) * @param bean the bean instance created, but whose properties have not yet been set * @param beanName the name of the bean - * @return the actual property values to apply to the given bean - * (can be the passed-in PropertyValues instance), or {@code null} - * to skip property population + * @return the actual property values to apply to the given bean (can be the passed-in + * PropertyValues instance), or {@code null} to skip property population * @throws org.springframework.beans.BeansException in case of errors + * @see #postProcessProperties * @see org.springframework.beans.MutablePropertyValues + * @deprecated as of 5.1, in favor of {@link #postProcessProperties(PropertyValues, Object, String)} */ + @Deprecated @Nullable default PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java index abe387968c..993056aec8 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java @@ -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. @@ -42,7 +42,7 @@ public abstract class InstantiationAwareBeanPostProcessorAdapter implements Smar @Override @Nullable - public Class predictBeanType(Class beanClass, String beanName) { + public Class predictBeanType(Class beanClass, String beanName) throws BeansException { return null; } @@ -68,6 +68,14 @@ public abstract class InstantiationAwareBeanPostProcessorAdapter implements Smar return true; } + @Override + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) + throws BeansException { + + return null; + } + + @Deprecated @Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 17f2e44046..b60b9f5463 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -1292,6 +1292,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * @param mbd the bean definition for the bean * @param bw the BeanWrapper with bean instance */ + @SuppressWarnings("deprecation") // for postProcessPropertyValues protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { if (bw == null) { if (mbd.hasPropertyValues()) { @@ -1347,25 +1348,33 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); - if (hasInstAwareBpps || needsDepCheck) { + PropertyDescriptor[] filteredPds = null; + if (hasInstAwareBpps) { if (pvs == null) { pvs = mbd.getPropertyValues(); } - PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); - if (hasInstAwareBpps) { - for (BeanPostProcessor bp : getBeanPostProcessors()) { - if (bp instanceof InstantiationAwareBeanPostProcessor) { - InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; - pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); - if (pvs == null) { + for (BeanPostProcessor bp : getBeanPostProcessors()) { + if (bp instanceof InstantiationAwareBeanPostProcessor) { + InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; + PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); + if (pvsToUse == null) { + if (filteredPds == null) { + filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); + } + pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); + if (pvsToUse == null) { return; } } + pvs = pvsToUse; } } - if (needsDepCheck) { - checkDependencies(beanName, mbd, filteredPds, pvs); + } + if (needsDepCheck) { + if (filteredPds == null) { + filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } + checkDependencies(beanName, mbd, filteredPds, pvs); } if (pvs != null) { @@ -1543,12 +1552,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor) */ protected void checkDependencies( - String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs) + String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, @Nullable PropertyValues pvs) throws UnsatisfiedDependencyException { int dependencyCheck = mbd.getDependencyCheck(); for (PropertyDescriptor pd : pds) { - if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) { + if (pd.getWriteMethod() != null && (pvs == null || !pvs.contains(pd.getName()))) { boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType()); boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) || (isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) || diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java index 7ca7861dc4..9851381cc6 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -39,6 +39,7 @@ import static org.junit.Assert.*; * @author Chris Beams * @since 2.0 */ +@Deprecated public class RequiredAnnotationBeanPostProcessorTests { @Test diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java index 3e3cd9bce9..583c9ff308 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java @@ -23,7 +23,6 @@ import java.util.Set; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; -import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.support.AbstractBeanDefinition; @@ -54,10 +53,9 @@ import org.springframework.util.ClassUtils; * @author Stephane Nicoll * @since 2.5 * @see ContextAnnotationAutowireCandidateResolver - * @see CommonAnnotationBeanPostProcessor * @see ConfigurationClassPostProcessor + * @see CommonAnnotationBeanPostProcessor * @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - * @see org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor * @see org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor */ public final class AnnotationConfigUtils { @@ -87,7 +85,9 @@ public final class AnnotationConfigUtils { /** * The bean name of the internally managed Required annotation processor. + * @deprecated as of 5.1, since no Required processor is registered by default anymore */ + @Deprecated public static final String REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalRequiredAnnotationProcessor"; @@ -103,7 +103,6 @@ public final class AnnotationConfigUtils { public static final String PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalPersistenceAnnotationProcessor"; - private static final String PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME = "org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"; @@ -174,12 +173,6 @@ public final class AnnotationConfigUtils { beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); } - if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) { - RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class); - def.setSource(source); - beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - } - // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor. if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) { RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index 98399344dc..bc8fdefdc5 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -47,7 +47,6 @@ import javax.xml.ws.WebServiceRef; import org.springframework.aop.TargetSource; import org.springframework.aop.framework.ProxyFactory; 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; @@ -300,19 +299,17 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean } @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 = findResourceMetadata(beanName, bean.getClass(), pvs); try { metadata.inject(bean, beanName, pvs); @@ -323,6 +320,14 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean return pvs; } + @Deprecated + @Override + public PropertyValues postProcessPropertyValues( + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { + + return postProcessProperties(pvs, bean, beanName); + } + private InjectionMetadata findResourceMetadata(String beanName, final Class clazz, @Nullable PropertyValues pvs) { // Fall back to class name as cache key, for backwards compatibility with custom callers. @@ -468,9 +473,11 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean * @param element the descriptor for the annotated field/method * @param requestingBeanName the name of the requesting bean * @return the resource object (never {@code null}) - * @throws BeansException if we failed to obtain the target resource + * @throws NoSuchBeanDefinitionException if no corresponding target resource found */ - protected Object getResource(LookupElement element, @Nullable String requestingBeanName) throws BeansException { + protected Object getResource(LookupElement element, @Nullable String requestingBeanName) + throws NoSuchBeanDefinitionException { + if (StringUtils.hasLength(element.mappedName)) { return this.jndiFactory.getBean(element.mappedName, element.lookupType); } @@ -491,10 +498,10 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean * @param element the descriptor for the annotated field/method * @param requestingBeanName the name of the requesting bean * @return the resource object (never {@code null}) - * @throws BeansException if we failed to obtain the target resource + * @throws NoSuchBeanDefinitionException if no corresponding target resource found */ protected Object autowireResource(BeanFactory factory, LookupElement element, @Nullable String requestingBeanName) - throws BeansException { + throws NoSuchBeanDefinitionException { Object resource; Set autowiredBeanNames; diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index 357b6ec579..2034fca30d 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -31,7 +31,6 @@ import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; import org.springframework.beans.factory.annotation.Autowire; -import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; @@ -171,6 +170,7 @@ class ConfigurationClassBeanDefinitionReader { * Read the given {@link BeanMethod}, registering bean definitions * with the BeanDefinitionRegistry based on its contents. */ + @SuppressWarnings("deprecation") // for RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) { ConfigurationClass configClass = beanMethod.getConfigurationClass(); MethodMetadata metadata = beanMethod.getMetadata(); @@ -222,7 +222,8 @@ class ConfigurationClassBeanDefinitionReader { beanDef.setUniqueFactoryMethodName(methodName); } beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); - beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); + beanDef.setAttribute(org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor. + SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index cdaefb9b33..9ddd100139 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -16,7 +16,6 @@ package org.springframework.context.annotation; -import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -426,11 +425,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo } @Override - public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { - + public PropertyValues postProcessProperties(@Nullable PropertyValues pvs, Object bean, String beanName) { // Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor's - // postProcessPropertyValues method attempts to autowire other configuration beans. + // postProcessProperties method attempts to autowire other configuration beans. if (bean instanceof EnhancedConfiguration) { ((EnhancedConfiguration) bean).setBeanFactory(this.beanFactory); } diff --git a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java index ddd93988f9..356eddca46 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java @@ -29,6 +29,7 @@ import org.springframework.aop.support.DelegatingIntroductionInterceptor; import org.springframework.asm.Type; import org.springframework.beans.BeanUtils; import org.springframework.beans.PropertyValue; +import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCurrentlyInCreationException; @@ -136,8 +137,8 @@ import org.springframework.util.StringUtils; * @author Mark Fisher * @since 2.0 */ -public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements - BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered { +public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProcessorAdapter + implements BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered { /** * The {@link org.springframework.core.io.Resource}-style prefix that denotes @@ -284,8 +285,8 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces if (ex instanceof BeanCreationException && ((BeanCreationException) ex).getMostSpecificCause() instanceof BeanCurrentlyInCreationException) { if (logger.isTraceEnabled()) { - logger.trace("Could not determine scripted object type for bean '" + beanName + "': " - + ex.getMessage()); + logger.trace("Could not determine scripted object type for bean '" + beanName + "': " + + ex.getMessage()); } } else { @@ -298,6 +299,11 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces return null; } + @Override + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { + return pvs; + } + @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) { // We only apply special treatment to ScriptFactory implementations here. diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ClassPathBeanDefinitionScannerTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ClassPathBeanDefinitionScannerTests.java index 4c51a747b9..754cfe1de7 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ClassPathBeanDefinitionScannerTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ClassPathBeanDefinitionScannerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -56,7 +56,7 @@ public class ClassPathBeanDefinitionScannerTests { GenericApplicationContext context = new GenericApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(13, beanCount); + assertEquals(12, beanCount); assertTrue(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("stubFooDao")); @@ -65,7 +65,6 @@ public class ClassPathBeanDefinitionScannerTests { assertTrue(context.containsBean("thoreau")); assertTrue(context.containsBean(AnnotationConfigUtils.CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -105,7 +104,7 @@ public class ClassPathBeanDefinitionScannerTests { GenericApplicationContext context = new GenericApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(13, beanCount); + assertEquals(12, beanCount); scanner.scan(BASE_PACKAGE); assertTrue(context.containsBean("serviceInvocationCounter")); @@ -230,10 +229,9 @@ public class ClassPathBeanDefinitionScannerTests { scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(7, beanCount); + assertEquals(6, beanCount); assertTrue(context.containsBean("messageBean")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -246,7 +244,7 @@ public class ClassPathBeanDefinitionScannerTests { scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(7, beanCount); + assertEquals(6, beanCount); assertTrue(context.containsBean("messageBean")); assertFalse(context.containsBean("serviceInvocationCounter")); assertFalse(context.containsBean("fooServiceImpl")); @@ -254,7 +252,6 @@ public class ClassPathBeanDefinitionScannerTests { assertFalse(context.containsBean("myNamedComponent")); assertFalse(context.containsBean("myNamedDao")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -267,7 +264,7 @@ public class ClassPathBeanDefinitionScannerTests { scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(14, beanCount); + assertEquals(13, beanCount); assertTrue(context.containsBean("messageBean")); assertTrue(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("fooServiceImpl")); @@ -275,7 +272,6 @@ public class ClassPathBeanDefinitionScannerTests { assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -288,14 +284,13 @@ public class ClassPathBeanDefinitionScannerTests { scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class)); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(12, beanCount); + assertEquals(11, beanCount); assertFalse(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("stubFooDao")); assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); } @@ -307,14 +302,13 @@ public class ClassPathBeanDefinitionScannerTests { scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(12, beanCount); + assertEquals(11, beanCount); assertFalse(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("stubFooDao")); assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -335,7 +329,6 @@ public class ClassPathBeanDefinitionScannerTests { assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertFalse(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertFalse(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertFalse(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); } @@ -347,14 +340,13 @@ public class ClassPathBeanDefinitionScannerTests { scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class)); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(11, beanCount); + assertEquals(10, beanCount); assertFalse(context.containsBean("fooServiceImpl")); assertFalse(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("stubFooDao")); assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -367,7 +359,7 @@ public class ClassPathBeanDefinitionScannerTests { scanner.setBeanNameGenerator(new TestBeanNameGenerator()); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(13, beanCount); + assertEquals(12, beanCount); assertFalse(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("fooService")); assertTrue(context.containsBean("serviceInvocationCounter")); @@ -375,7 +367,6 @@ public class ClassPathBeanDefinitionScannerTests { assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); - assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); @@ -388,7 +379,7 @@ public class ClassPathBeanDefinitionScannerTests { GenericApplicationContext multiPackageContext = new GenericApplicationContext(); ClassPathBeanDefinitionScanner multiPackageScanner = new ClassPathBeanDefinitionScanner(multiPackageContext); int singlePackageBeanCount = singlePackageScanner.scan(BASE_PACKAGE); - assertEquals(13, singlePackageBeanCount); + assertEquals(12, singlePackageBeanCount); multiPackageScanner.scan(BASE_PACKAGE, "org.springframework.dao.annotation"); // assertTrue(multiPackageBeanCount > singlePackageBeanCount); } @@ -399,7 +390,7 @@ public class ClassPathBeanDefinitionScannerTests { ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); int initialBeanCount = context.getBeanDefinitionCount(); int scannedBeanCount = scanner.scan(BASE_PACKAGE); - assertEquals(13, scannedBeanCount); + assertEquals(12, scannedBeanCount); assertEquals(scannedBeanCount, context.getBeanDefinitionCount() - initialBeanCount); int addedBeanCount = scanner.scan("org.springframework.aop.aspectj.annotation"); assertEquals(initialBeanCount + scannedBeanCount + addedBeanCount, context.getBeanDefinitionCount()); @@ -412,7 +403,7 @@ public class ClassPathBeanDefinitionScannerTests { ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); scanner.setBeanNameGenerator(new TestBeanNameGenerator()); int beanCount = scanner.scan(BASE_PACKAGE); - assertEquals(13, beanCount); + assertEquals(12, beanCount); context.refresh(); FooServiceImpl fooService = context.getBean("fooService", FooServiceImpl.class); diff --git a/spring-context/src/test/java/org/springframework/context/annotation/Spr8954Tests.java b/spring-context/src/test/java/org/springframework/context/annotation/Spr8954Tests.java index c321cdbeac..4bb5f21edd 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/Spr8954Tests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/Spr8954Tests.java @@ -20,6 +20,7 @@ import java.util.Map; import org.junit.Test; +import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -90,10 +91,11 @@ public class Spr8954Tests { } } + static class FooFactoryBean implements FactoryBean, AnInterface { @Override - public Foo getObject() throws Exception { + public Foo getObject() { return new Foo(); } @@ -108,24 +110,29 @@ public class Spr8954Tests { } } + interface AnInterface { } + static class Foo { } + interface PredictedType { } - static class PredictedTypeImpl implements PredictedType { - } static class PredictingBPP extends InstantiationAwareBeanPostProcessorAdapter { @Override public Class predictBeanType(Class beanClass, String beanName) { - return FactoryBean.class.isAssignableFrom(beanClass) ? - PredictedType.class : null; + return FactoryBean.class.isAssignableFrom(beanClass) ? PredictedType.class : null; + } + + @Override + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { + return pvs; } } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java index 6ac6bdea58..5d172cbb01 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -35,8 +35,6 @@ import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Required; -import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; @@ -279,9 +277,6 @@ public class ConfigurationClassProcessingTests { ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor(); ccpp.postProcessBeanDefinitionRegistry(factory); ccpp.postProcessBeanFactory(factory); - RequiredAnnotationBeanPostProcessor rapp = new RequiredAnnotationBeanPostProcessor(); - rapp.setBeanFactory(factory); - factory.addBeanPostProcessor(rapp); factory.freezeConfiguration(); return factory; } @@ -514,7 +509,6 @@ public class ConfigurationClassProcessingTests { } @Override - @Required public void setSpouse(ITestBean spouse) { super.setSpouse(spouse); } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java index 1e7583a7a6..836ddf7066 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java @@ -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); } diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java index e91833b6b9..f2b1fcc4de 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java @@ -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()); diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java index 31936b3b79..22503cc3dc 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java @@ -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);