Post-processors consistently ignore ScopedObject/AopInfrastructureBean

Issue: SPR-17166
This commit is contained in:
Juergen Hoeller
2018-08-12 14:36:20 +02:00
parent 6ed03c24ac
commit d8aecd8c87
3 changed files with 51 additions and 38 deletions

View File

@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.AopInfrastructureBean;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
@@ -98,10 +99,10 @@ public class JmsListenerAnnotationBeanPostProcessor
protected final Log logger = LogFactory.getLog(getClass());
@Nullable
private JmsListenerEndpointRegistry endpointRegistry;
private String containerFactoryBeanName = DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
@Nullable
private String containerFactoryBeanName = DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
private JmsListenerEndpointRegistry endpointRegistry;
private final MessageHandlerMethodFactoryAdapter messageHandlerMethodFactory =
new MessageHandlerMethodFactoryAdapter();
@@ -124,14 +125,6 @@ public class JmsListenerAnnotationBeanPostProcessor
return LOWEST_PRECEDENCE;
}
/**
* Set the {@link JmsListenerEndpointRegistry} that will hold the created
* endpoint and manage the lifecycle of the related listener container.
*/
public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
this.endpointRegistry = endpointRegistry;
}
/**
* Set the name of the {@link JmsListenerContainerFactory} to use by default.
* <p>If none is specified, "jmsListenerContainerFactory" is assumed to be defined.
@@ -140,6 +133,14 @@ public class JmsListenerAnnotationBeanPostProcessor
this.containerFactoryBeanName = containerFactoryBeanName;
}
/**
* Set the {@link JmsListenerEndpointRegistry} that will hold the created
* endpoint and manage the lifecycle of the related listener container.
*/
public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
this.endpointRegistry = endpointRegistry;
}
/**
* Set the {@link MessageHandlerMethodFactory} to use to configure the message
* listener responsible to serve an endpoint detected by this processor.
@@ -183,6 +184,10 @@ public class JmsListenerAnnotationBeanPostProcessor
}
}
if (this.containerFactoryBeanName != null) {
this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
}
if (this.registrar.getEndpointRegistry() == null) {
// Determine JmsListenerEndpointRegistry bean from the BeanFactory
if (this.endpointRegistry == null) {
@@ -193,9 +198,6 @@ public class JmsListenerAnnotationBeanPostProcessor
this.registrar.setEndpointRegistry(this.endpointRegistry);
}
if (this.containerFactoryBeanName != null) {
this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
}
// Set the custom handler method factory once resolved by the configurer
MessageHandlerMethodFactory handlerMethodFactory = this.registrar.getMessageHandlerMethodFactory();
@@ -218,9 +220,14 @@ public class JmsListenerAnnotationBeanPostProcessor
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AopInfrastructureBean) {
// Ignore AOP infrastructure such as scoped proxies.
return bean;
}
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
if (!this.nonAnnotatedClasses.contains(targetClass)) {
Map<Method, Set<JmsListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
(MethodIntrospector.MetadataLookup<Set<JmsListener>>) method -> {
Set<JmsListener> listenerMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
@@ -228,16 +235,15 @@ public class JmsListenerAnnotationBeanPostProcessor
return (!listenerMethods.isEmpty() ? listenerMethods : null);
});
if (annotatedMethods.isEmpty()) {
this.nonAnnotatedClasses.add(bean.getClass());
this.nonAnnotatedClasses.add(targetClass);
if (logger.isTraceEnabled()) {
logger.trace("No @JmsListener annotations found on bean type: " + bean.getClass());
logger.trace("No @JmsListener annotations found on bean type: " + targetClass);
}
}
else {
// Non-empty set of methods
annotatedMethods.forEach((method, listeners) ->
listeners.forEach(listener ->
processJmsListener(listener, method, bean)));
listeners.forEach(listener -> processJmsListener(listener, method, bean)));
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName +
"': " + annotatedMethods);