PublisherAnnotationPostProcessor now checks if any @Publisher annotations are defined on class methods only (rather than interfaces), and if so, the proxy will set the 'proxyTargetClass' property to 'true' prior to creating the proxy (INT-263).

This commit is contained in:
Mark Fisher
2008-08-19 15:43:35 +00:00
parent 8432e84e10
commit 44e0c1c0c0
3 changed files with 32 additions and 2 deletions

View File

@@ -17,6 +17,8 @@
package org.springframework.integration.config.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
@@ -25,10 +27,12 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.annotation.Publisher;
import org.springframework.integration.aop.PublisherAnnotationAdvisor;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* A {@link BeanPostProcessor} that adds a message publishing interceptor when
@@ -96,6 +100,7 @@ public class PublisherAnnotationPostProcessor implements BeanPostProcessor, Bean
}
else {
ProxyFactory pf = new ProxyFactory(bean);
pf.setProxyTargetClass(this.requiresClassProxying(targetClass));
pf.addAdvisor(this.advisor);
return pf.getProxy(this.beanClassLoader);
}
@@ -105,4 +110,29 @@ public class PublisherAnnotationPostProcessor implements BeanPostProcessor, Bean
}
}
private boolean requiresClassProxying(Class<?> targetClass) {
final AtomicBoolean result = new AtomicBoolean(false);
final Class<?>[] interfaces = targetClass.getInterfaces();
ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Annotation annotation = AnnotationUtils.findAnnotation(method, publisherAnnotationType);
if (annotation != null) {
boolean foundMethodOnInterface = false;
for (Class<?> iface : interfaces) {
Method ifaceMethod = ReflectionUtils.findMethod(
iface, method.getName(), method.getParameterTypes());
if (ifaceMethod != null) {
foundMethodOnInterface = true;
break;
}
}
if (!foundMethodOnInterface) {
result.set(true);
}
}
}
});
return result.get();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 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.