Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #532]fix: Custom CGLIB classes cannot be registered #533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package org.apache.rocketmq.spring.annotation;

import org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.OrderComparator;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils;

import java.lang.reflect.AnnotatedElement;
import java.util.List;
Expand All @@ -47,7 +48,10 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class<?> targetClass = AopUtils.getTargetClass(bean);
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
if (targetClass.getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)) {
targetClass = ClassUtils.getUserClass(bean);
}
RocketMQMessageListener ann = targetClass.getAnnotation(RocketMQMessageListener.class);
if (ann != null) {
RocketMQMessageListener enhance = enhance(targetClass, ann);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@

import org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
Expand All @@ -47,6 +54,15 @@ public void testAnnotationEnhancer() {

}

@Test
public void testGetTargetClass() {
runner.withPropertyValues("rocketmq.name-server=127.0.0.1:9876").
withUserConfiguration(TestAnnotationEnhancerConfig.class, TestGetTargetClassConfig.class).
run((context) -> {
assertThat(context).getFailure().hasMessageContaining("connect to null failed");
});
}

@Configuration
static class TestAnnotationEnhancerConfig {
@Bean
Expand All @@ -65,6 +81,17 @@ public RocketMQMessageListenerBeanPostProcessor.AnnotationEnhancer consumeContai
}
}

@Configuration
static class TestGetTargetClassConfig {
@Bean
public Receiver customEnhancer() {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Receiver.class);
enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> null);
return (Receiver) enhancer.create();
}
}

@Configuration
static class TestReceiverConfig {
@Bean
Expand Down