Skip to content

Commit d8aecd8

Browse files
committed
Post-processors consistently ignore ScopedObject/AopInfrastructureBean
Issue: SPR-17166
1 parent 6ed03c2 commit d8aecd8

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
330330
if (annotatedMethods.isEmpty()) {
331331
this.nonAnnotatedClasses.add(targetClass);
332332
if (logger.isTraceEnabled()) {
333-
logger.trace("No @Scheduled annotations found on bean class: " + bean.getClass());
333+
logger.trace("No @Scheduled annotations found on bean class: " + targetClass);
334334
}
335335
}
336336
else {
@@ -354,8 +354,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
354354
*/
355355
protected void processScheduled(Scheduled scheduled, Method method, Object bean) {
356356
try {
357-
Assert.isTrue(method.getParameterCount() == 0,
358-
"Only no-arg methods may be annotated with @Scheduled");
357+
Assert.isTrue(method.getParameterCount() == 0, "Only no-arg methods may be annotated with @Scheduled");
359358

360359
Method invocableMethod = AopUtils.selectInvocableMethod(method, bean.getClass());
361360
Runnable runnable = new ScheduledMethodRunnable(bean, invocableMethod);

spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
3030

31+
import org.springframework.aop.framework.AopInfrastructureBean;
3132
import org.springframework.aop.framework.AopProxyUtils;
3233
import org.springframework.aop.support.AopUtils;
3334
import org.springframework.beans.BeansException;
@@ -98,10 +99,10 @@ public class JmsListenerAnnotationBeanPostProcessor
9899
protected final Log logger = LogFactory.getLog(getClass());
99100

100101
@Nullable
101-
private JmsListenerEndpointRegistry endpointRegistry;
102+
private String containerFactoryBeanName = DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
102103

103104
@Nullable
104-
private String containerFactoryBeanName = DEFAULT_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
105+
private JmsListenerEndpointRegistry endpointRegistry;
105106

106107
private final MessageHandlerMethodFactoryAdapter messageHandlerMethodFactory =
107108
new MessageHandlerMethodFactoryAdapter();
@@ -124,14 +125,6 @@ public int getOrder() {
124125
return LOWEST_PRECEDENCE;
125126
}
126127

127-
/**
128-
* Set the {@link JmsListenerEndpointRegistry} that will hold the created
129-
* endpoint and manage the lifecycle of the related listener container.
130-
*/
131-
public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
132-
this.endpointRegistry = endpointRegistry;
133-
}
134-
135128
/**
136129
* Set the name of the {@link JmsListenerContainerFactory} to use by default.
137130
* <p>If none is specified, "jmsListenerContainerFactory" is assumed to be defined.
@@ -140,6 +133,14 @@ public void setContainerFactoryBeanName(String containerFactoryBeanName) {
140133
this.containerFactoryBeanName = containerFactoryBeanName;
141134
}
142135

136+
/**
137+
* Set the {@link JmsListenerEndpointRegistry} that will hold the created
138+
* endpoint and manage the lifecycle of the related listener container.
139+
*/
140+
public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
141+
this.endpointRegistry = endpointRegistry;
142+
}
143+
143144
/**
144145
* Set the {@link MessageHandlerMethodFactory} to use to configure the message
145146
* listener responsible to serve an endpoint detected by this processor.
@@ -183,6 +184,10 @@ public void afterSingletonsInstantiated() {
183184
}
184185
}
185186

187+
if (this.containerFactoryBeanName != null) {
188+
this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
189+
}
190+
186191
if (this.registrar.getEndpointRegistry() == null) {
187192
// Determine JmsListenerEndpointRegistry bean from the BeanFactory
188193
if (this.endpointRegistry == null) {
@@ -193,9 +198,6 @@ public void afterSingletonsInstantiated() {
193198
this.registrar.setEndpointRegistry(this.endpointRegistry);
194199
}
195200

196-
if (this.containerFactoryBeanName != null) {
197-
this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
198-
}
199201

200202
// Set the custom handler method factory once resolved by the configurer
201203
MessageHandlerMethodFactory handlerMethodFactory = this.registrar.getMessageHandlerMethodFactory();
@@ -218,26 +220,30 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
218220
}
219221

220222
@Override
221-
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
222-
if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
223-
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
223+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
224+
if (bean instanceof AopInfrastructureBean) {
225+
// Ignore AOP infrastructure such as scoped proxies.
226+
return bean;
227+
}
228+
229+
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
230+
if (!this.nonAnnotatedClasses.contains(targetClass)) {
224231
Map<Method, Set<JmsListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
225232
(MethodIntrospector.MetadataLookup<Set<JmsListener>>) method -> {
226233
Set<JmsListener> listenerMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
227234
method, JmsListener.class, JmsListeners.class);
228235
return (!listenerMethods.isEmpty() ? listenerMethods : null);
229236
});
230237
if (annotatedMethods.isEmpty()) {
231-
this.nonAnnotatedClasses.add(bean.getClass());
238+
this.nonAnnotatedClasses.add(targetClass);
232239
if (logger.isTraceEnabled()) {
233-
logger.trace("No @JmsListener annotations found on bean type: " + bean.getClass());
240+
logger.trace("No @JmsListener annotations found on bean type: " + targetClass);
234241
}
235242
}
236243
else {
237244
// Non-empty set of methods
238245
annotatedMethods.forEach((method, listeners) ->
239-
listeners.forEach(listener ->
240-
processJmsListener(listener, method, bean)));
246+
listeners.forEach(listener -> processJmsListener(listener, method, bean)));
241247
if (logger.isDebugEnabled()) {
242248
logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName +
243249
"': " + annotatedMethods);

spring-jms/src/test/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessorTests.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public void simpleMessageListener() throws Exception {
7474
assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass());
7575
MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint;
7676
assertEquals(SimpleMessageListenerTestBean.class, methodEndpoint.getBean().getClass());
77-
assertEquals(SimpleMessageListenerTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMethod());
78-
assertEquals(SimpleMessageListenerTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMostSpecificMethod());
77+
assertEquals(SimpleMessageListenerTestBean.class.getMethod("handleIt", String.class),
78+
methodEndpoint.getMethod());
79+
assertEquals(SimpleMessageListenerTestBean.class.getMethod("handleIt", String.class),
80+
methodEndpoint.getMostSpecificMethod());
7981

8082
SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
8183
methodEndpoint.setupListenerContainer(listenerContainer);
@@ -99,8 +101,10 @@ public void metaAnnotationIsDiscovered() throws Exception {
99101
assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass());
100102
MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint;
101103
assertEquals(MetaAnnotationTestBean.class, methodEndpoint.getBean().getClass());
102-
assertEquals(MetaAnnotationTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMethod());
103-
assertEquals(MetaAnnotationTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMostSpecificMethod());
104+
assertEquals(MetaAnnotationTestBean.class.getMethod("handleIt", String.class),
105+
methodEndpoint.getMethod());
106+
assertEquals(MetaAnnotationTestBean.class.getMethod("handleIt", String.class),
107+
methodEndpoint.getMostSpecificMethod());
104108
assertEquals("metaTestQueue", ((AbstractJmsListenerEndpoint) endpoint).getDestination());
105109
}
106110
finally {
@@ -121,12 +125,14 @@ public void sendToAnnotationFoundOnInterfaceProxy() throws Exception {
121125
MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint;
122126
assertTrue(AopUtils.isJdkDynamicProxy(methodEndpoint.getBean()));
123127
assertTrue(methodEndpoint.getBean() instanceof SimpleService);
124-
assertEquals(SimpleService.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMethod());
125-
assertEquals(InterfaceProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMostSpecificMethod());
126-
127-
Method m = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination");
128-
ReflectionUtils.makeAccessible(m);
129-
Object destination = ReflectionUtils.invokeMethod(m, endpoint);
128+
assertEquals(SimpleService.class.getMethod("handleIt", String.class, String.class),
129+
methodEndpoint.getMethod());
130+
assertEquals(InterfaceProxyTestBean.class.getMethod("handleIt", String.class, String.class),
131+
methodEndpoint.getMostSpecificMethod());
132+
133+
Method method = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination");
134+
ReflectionUtils.makeAccessible(method);
135+
Object destination = ReflectionUtils.invokeMethod(method, endpoint);
130136
assertEquals("SendTo annotation not found on proxy", "foobar", destination);
131137
}
132138
finally {
@@ -147,12 +153,14 @@ public void sendToAnnotationFoundOnCglibProxy() throws Exception {
147153
MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint;
148154
assertTrue(AopUtils.isCglibProxy(methodEndpoint.getBean()));
149155
assertTrue(methodEndpoint.getBean() instanceof ClassProxyTestBean);
150-
assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMethod());
151-
assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMostSpecificMethod());
152-
153-
Method m = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination");
154-
ReflectionUtils.makeAccessible(m);
155-
Object destination = ReflectionUtils.invokeMethod(m, endpoint);
156+
assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class),
157+
methodEndpoint.getMethod());
158+
assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class),
159+
methodEndpoint.getMostSpecificMethod());
160+
161+
Method method = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination");
162+
ReflectionUtils.makeAccessible(method);
163+
Object destination = ReflectionUtils.invokeMethod(method, endpoint);
156164
assertEquals("SendTo annotation not found on proxy", "foobar", destination);
157165
}
158166
finally {
@@ -201,8 +209,8 @@ static class Config {
201209
@Bean
202210
public JmsListenerAnnotationBeanPostProcessor postProcessor() {
203211
JmsListenerAnnotationBeanPostProcessor postProcessor = new JmsListenerAnnotationBeanPostProcessor();
204-
postProcessor.setEndpointRegistry(jmsListenerEndpointRegistry());
205212
postProcessor.setContainerFactoryBeanName("testFactory");
213+
postProcessor.setEndpointRegistry(jmsListenerEndpointRegistry());
206214
return postProcessor;
207215
}
208216

0 commit comments

Comments
 (0)