1、实现对Controller接口的动态代理
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyController {
String value() default "";
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyControllerMethod {
String value() default "";
Class beanType() default Object.class;
String methodName() default "";
}
@RestController
public interface TestMyController {
@GetMapping("/my/hello")
@MyControllerMethod(beanType = TestMyService.class, methodName = "hello")
String hello();
}
@Service
public class TestMyService {
String hello(){
return "hello";
}
}
@Component
@Slf4j
public class ServiceBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor, ResourceLoaderAware, ApplicationContextAware {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("BeanDefinitionRegistryPostProcessor,postProcessBeanDefinitionRegistry");
Set<Class<?>> clazzSet = scannerPackages("io.github.ygsama.microservice");
for (Class beanClazz : clazzSet) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClazz);
GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
definition.getConstructorArgumentValues().addGenericArgumentValue(beanClazz);
definition.getConstructorArgumentValues().addGenericArgumentValue(applicationContext);