diff --git a/README.md b/README.md index c2be2a6..0295a07 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ @Component public class InvocationDispatcherImpl extends AbstractInvocationDispatcher { @Override - protected Object invoke(StubContext stubContext, Object proxy, Method method, Object[] args) throws Throwable { - System.out.println(stubContext.getAnnotation()); + protected Object invoke(StubContext stubProxyContext, Object proxy, Method method, Object[] args) throws Throwable { + System.out.println(stubProxyContext.getAnnotation()); System.out.println("InvocationDispatcherImpl"); return null; } @@ -75,8 +75,8 @@ public class HelloServiceTest { @Component public class InvocationDispatcherImpl extends AbstractInvocationDispatcher { @Override - protected Object invoke(StubContext stubContext, Object proxy, Method method, Object[] args) throws Throwable { - System.out.println(stubContext.getAnnotation()); + protected Object invoke(StubContext stubProxyContext, Object proxy, Method method, Object[] args) throws Throwable { + System.out.println(stubProxyContext.getAnnotation()); System.out.println("InvocationDispatcherImpl"); return null; } diff --git a/pom.xml b/pom.xml index fc14010..1e5c331 100644 --- a/pom.xml +++ b/pom.xml @@ -5,8 +5,8 @@ com.github.yungyu16.spring spring-boot-starter-proxy - 1.0.0 - ${project.groupId}:${project.artifactId} + 1.1.0 + ${project.artifactId} 将不可实例化的Interface动态代理后注册到Spring容器以便IOC,用于便捷的生成Local Stub https://github.com/yungyu16/version-maven-plugin.git @@ -56,18 +56,12 @@ Github Issues https://github.com/yungyu16/spring-boot-starter-proxy/issues - org.projectlombok lombok provided - - org.springframework.boot - spring-boot-starter-test - test - org.springframework.boot spring-boot-starter @@ -78,6 +72,11 @@ spring-boot-starter-logging true + + org.springframework.boot + spring-boot-starter-test + test + @@ -91,6 +90,11 @@ + + com.github.yungyu16.maven + version-maven-plugin + 1.0.1 + org.apache.maven.plugins diff --git a/src/main/java/com/github/yungyu16/spring/proxy/StubBeanPostProcessor.java b/src/main/java/com/github/yungyu16/spring/proxy/StubBeanPostProcessor.java deleted file mode 100644 index 1772e46..0000000 --- a/src/main/java/com/github/yungyu16/spring/proxy/StubBeanPostProcessor.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.github.yungyu16.spring.proxy; - -import com.github.yungyu16.spring.proxy.annotation.ProxyStub; -import lombok.NonNull; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.BeanCreationNotAllowedException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; -import org.springframework.core.annotation.AnnotatedElementUtils; -import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.util.ClassUtils; -import org.springframework.util.ReflectionUtils; - -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -/** - * CreatedDate: 2020/11/24 - * Author: songjialin - */ -public class StubBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware { - private BeanFactory beanFactory; - - @Override - @SuppressWarnings("all") - public Object postProcessBeforeInstantiation(Class type, String name) throws BeansException { - ProxyStub proxyStub = AnnotatedElementUtils.getMergedAnnotation(type, ProxyStub.class); - if (proxyStub == null) { - return null; - } - if (!type.isInterface()) { - throw new BeanCreationNotAllowedException(name, type.getName() + " 不是Interface"); - } - AbstractInvocationDispatcher invocationDispatcher = getInvocationDispatcher(type, proxyStub); - Class annotationType = invocationDispatcher.getAnnotationType(); - Annotation annotation = AnnotationUtils.findAnnotation(type, annotationType); - StubContext stubContext = StubContext.valueOf(type, annotation); - return Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), new Class[]{type, StubLabel.class}, StubInvocationHandler.newInstance(stubContext, invocationDispatcher)); - } - - @SuppressWarnings("all") - private AbstractInvocationDispatcher getInvocationDispatcher(@NonNull Class type, @NonNull ProxyStub proxyStub) { - Class beanType = proxyStub.dispatcherType(); - - Object handler = null; - if (beanType != AbstractInvocationDispatcher.class) { - handler = beanFactory.getBean(beanType); - } else { - throw new BeanCreationException(type.getName() + " 没有指定InvocationDispatcher"); - } - if (!(handler instanceof AbstractInvocationDispatcher)) { - throw new BeanCreationException(type.getName() + " InvocationDispatcher类型错误"); - } - return (AbstractInvocationDispatcher) handler; - } - - @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - this.beanFactory = beanFactory; - } - - @SuppressWarnings("rawtypes") - static class StubInvocationHandler implements InvocationHandler { - - public static StubInvocationHandler newInstance(@NonNull StubContext stubContext, @NonNull AbstractInvocationDispatcher dispatcher) { - return new StubInvocationHandler(stubContext, dispatcher); - } - - private final StubContext stubContext; - private final AbstractInvocationDispatcher dispatcher; - - private StubInvocationHandler(StubContext stubContext, AbstractInvocationDispatcher dispatcher) { - this.stubContext = stubContext; - this.dispatcher = dispatcher; - } - - @Override - @SuppressWarnings("unchecked") - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - if (ReflectionUtils.isToStringMethod(method)) { - return "ProxyStub:" + ClassUtils.classNamesToString(stubContext.getStubType()) + ":" + stubContext.getAnnotation(); - } - if (ReflectionUtils.isEqualsMethod(method) - || ReflectionUtils.isHashCodeMethod(method)) { - return method.invoke(this, args); - } - return dispatcher.invoke(stubContext, proxy, method, args); - } - } -} diff --git a/src/main/java/com/github/yungyu16/spring/proxy/StubContext.java b/src/main/java/com/github/yungyu16/spring/proxy/StubContext.java deleted file mode 100644 index cde2caf..0000000 --- a/src/main/java/com/github/yungyu16/spring/proxy/StubContext.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.github.yungyu16.spring.proxy; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NonNull; -import lombok.ToString; - -import java.lang.annotation.Annotation; - -/** - * @author Yungyu - * @description Created by Yungyu on 2020/11/24. - */ -@Getter -@EqualsAndHashCode -@ToString -public class StubContext { - private final T annotation; - private final Class stubType; - - private StubContext(@NonNull Class stubType, T annotation) { - this.annotation = annotation; - this.stubType = stubType; - } - - public static StubContext valueOf(Class stubType, T annotation) { - return new StubContext<>(stubType, annotation); - } - -} diff --git a/src/main/java/com/github/yungyu16/spring/proxy/StubBeanAutoConfiguration.java b/src/main/java/com/github/yungyu16/spring/stub/StubBeanAutoConfiguration.java similarity index 66% rename from src/main/java/com/github/yungyu16/spring/proxy/StubBeanAutoConfiguration.java rename to src/main/java/com/github/yungyu16/spring/stub/StubBeanAutoConfiguration.java index 003a412..4a841aa 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/StubBeanAutoConfiguration.java +++ b/src/main/java/com/github/yungyu16/spring/stub/StubBeanAutoConfiguration.java @@ -1,5 +1,6 @@ -package com.github.yungyu16.spring.proxy; +package com.github.yungyu16.spring.stub; +import com.github.yungyu16.spring.stub.proxy.DefaultStubProxyFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -18,4 +19,9 @@ public StubDefPostProcessor stubBeanFactoryPostProcessor() { public StubBeanPostProcessor stubBeanPostProcessor() { return new StubBeanPostProcessor(); } + + @Bean + public DefaultStubProxyFactory defaultStubProxyFactory() { + return new DefaultStubProxyFactory(); + } } diff --git a/src/main/java/com/github/yungyu16/spring/proxy/StubBeanDefinitionRegistrar.java b/src/main/java/com/github/yungyu16/spring/stub/StubBeanDefinitionRegistrar.java similarity index 91% rename from src/main/java/com/github/yungyu16/spring/proxy/StubBeanDefinitionRegistrar.java rename to src/main/java/com/github/yungyu16/spring/stub/StubBeanDefinitionRegistrar.java index 472ca70..c3995c2 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/StubBeanDefinitionRegistrar.java +++ b/src/main/java/com/github/yungyu16/spring/stub/StubBeanDefinitionRegistrar.java @@ -1,7 +1,7 @@ -package com.github.yungyu16.spring.proxy; +package com.github.yungyu16.spring.stub; -import com.github.yungyu16.spring.proxy.annotation.ProxyStubScan; -import com.github.yungyu16.spring.proxy.support.ClassPathStubBeanDefinitionScanner; +import com.github.yungyu16.spring.stub.annotation.ProxyStubScan; +import com.github.yungyu16.spring.stub.support.ClassPathStubBeanDefinitionScanner; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.EnvironmentAware; diff --git a/src/main/java/com/github/yungyu16/spring/stub/StubBeanPostProcessor.java b/src/main/java/com/github/yungyu16/spring/stub/StubBeanPostProcessor.java new file mode 100644 index 0000000..d643114 --- /dev/null +++ b/src/main/java/com/github/yungyu16/spring/stub/StubBeanPostProcessor.java @@ -0,0 +1,42 @@ +package com.github.yungyu16.spring.stub; + +import com.github.yungyu16.spring.stub.annotation.ProxyStub; +import com.github.yungyu16.spring.stub.proxy.StubProxyFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanCreationNotAllowedException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; +import org.springframework.core.annotation.AnnotatedElementUtils; + +/** + * CreatedDate: 2020/11/24 + * Author: songjialin + */ +public class StubBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware { + private BeanFactory beanFactory; + + @Override + @SuppressWarnings("all") + public Object postProcessBeforeInstantiation(Class type, String name) throws BeansException { + ProxyStub proxyStub = AnnotatedElementUtils.getMergedAnnotation(type, ProxyStub.class); + if (proxyStub == null) { + return null; + } + if (!type.isInterface()) { + throw new BeanCreationNotAllowedException(name, type.getName() + " 不是Interface"); + } + StubProxyFactory stubProxyFactory = getStubProxyFactory(proxyStub); + return stubProxyFactory.createProxy(type, proxyStub); + } + + private StubProxyFactory getStubProxyFactory(ProxyStub proxyStub) { + Class factoryType = proxyStub.factoryType(); + return beanFactory.getBean(factoryType); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } +} diff --git a/src/main/java/com/github/yungyu16/spring/proxy/StubDefPostProcessor.java b/src/main/java/com/github/yungyu16/spring/stub/StubDefPostProcessor.java similarity index 89% rename from src/main/java/com/github/yungyu16/spring/proxy/StubDefPostProcessor.java rename to src/main/java/com/github/yungyu16/spring/stub/StubDefPostProcessor.java index 2e9f1c4..d0838c0 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/StubDefPostProcessor.java +++ b/src/main/java/com/github/yungyu16/spring/stub/StubDefPostProcessor.java @@ -1,7 +1,7 @@ -package com.github.yungyu16.spring.proxy; +package com.github.yungyu16.spring.stub; -import com.github.yungyu16.spring.proxy.support.BeanDefinitionRegistryPostProcessorAdapter; -import com.github.yungyu16.spring.proxy.support.ClassPathStubBeanDefinitionScanner; +import com.github.yungyu16.spring.stub.support.BeanDefinitionRegistryPostProcessorAdapter; +import com.github.yungyu16.spring.stub.support.ClassPathStubBeanDefinitionScanner; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; diff --git a/src/main/java/com/github/yungyu16/spring/proxy/annotation/ProxyStub.java b/src/main/java/com/github/yungyu16/spring/stub/annotation/ProxyStub.java similarity index 52% rename from src/main/java/com/github/yungyu16/spring/proxy/annotation/ProxyStub.java rename to src/main/java/com/github/yungyu16/spring/stub/annotation/ProxyStub.java index 17e0685..446c78f 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/annotation/ProxyStub.java +++ b/src/main/java/com/github/yungyu16/spring/stub/annotation/ProxyStub.java @@ -1,6 +1,8 @@ -package com.github.yungyu16.spring.proxy.annotation; +package com.github.yungyu16.spring.stub.annotation; -import com.github.yungyu16.spring.proxy.AbstractInvocationDispatcher; +import com.github.yungyu16.spring.stub.proxy.AbstractInvocationDispatcher; +import com.github.yungyu16.spring.stub.proxy.DefaultStubProxyFactory; +import com.github.yungyu16.spring.stub.proxy.StubProxyFactory; import org.springframework.core.annotation.AliasFor; import org.springframework.stereotype.Component; @@ -24,7 +26,15 @@ String beanName() default ""; /** - * 指定动态代理调用拦截器BeanType,用于从BeanFactory中按类型获取bean + * 指定动态代理工厂,用于定制动态代理方案比如Cglib、ByteBuddy等 + * 默认使用基于JdkDynamicProxy代理并使用AbstractInvocationDispatcher拦截方法调用 + * + * @return + */ + Class factoryType() default DefaultStubProxyFactory.class; + + /** + * 指定JdkDynamicProxy调用拦截器BeanType,用于从BeanFactory中按类型获取bean * * @return */ @@ -32,7 +42,7 @@ Class value() default AbstractInvocationDispatcher.class; /** - * 指定动态代理调用拦截器BeanType,用于从BeanFactory中按类型获取bean + * 指定JdkDynamicProxy调用拦截器BeanType,用于从BeanFactory中按类型获取bean * * @return */ diff --git a/src/main/java/com/github/yungyu16/spring/proxy/annotation/ProxyStubScan.java b/src/main/java/com/github/yungyu16/spring/stub/annotation/ProxyStubScan.java similarity index 81% rename from src/main/java/com/github/yungyu16/spring/proxy/annotation/ProxyStubScan.java rename to src/main/java/com/github/yungyu16/spring/stub/annotation/ProxyStubScan.java index dd8f42a..1de2a29 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/annotation/ProxyStubScan.java +++ b/src/main/java/com/github/yungyu16/spring/stub/annotation/ProxyStubScan.java @@ -1,6 +1,6 @@ -package com.github.yungyu16.spring.proxy.annotation; +package com.github.yungyu16.spring.stub.annotation; -import com.github.yungyu16.spring.proxy.StubBeanDefinitionRegistrar; +import com.github.yungyu16.spring.stub.StubBeanDefinitionRegistrar; import org.springframework.context.annotation.Import; import org.springframework.core.annotation.AliasFor; diff --git a/src/main/java/com/github/yungyu16/spring/proxy/AbstractInvocationDispatcher.java b/src/main/java/com/github/yungyu16/spring/stub/proxy/AbstractInvocationDispatcher.java similarity index 70% rename from src/main/java/com/github/yungyu16/spring/proxy/AbstractInvocationDispatcher.java rename to src/main/java/com/github/yungyu16/spring/stub/proxy/AbstractInvocationDispatcher.java index 30726f3..44116ca 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/AbstractInvocationDispatcher.java +++ b/src/main/java/com/github/yungyu16/spring/stub/proxy/AbstractInvocationDispatcher.java @@ -1,6 +1,9 @@ -package com.github.yungyu16.spring.proxy; +package com.github.yungyu16.spring.stub.proxy; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NonNull; +import lombok.ToString; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -51,12 +54,29 @@ Class getAnnotationType() { return (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } - protected Object invoke(StubContext stubContext, Object proxy, Method method, Object[] args) throws Throwable { + protected Object invoke(StubProxyContext stubProxyContext, Object proxy, Method method, Object[] args) throws Throwable { return invoke(proxy, method, args); } protected Object invoke(Object proxy, Method method, Object[] args) throws Throwable { throw new UnsupportedOperationException(); } + + @Getter + @EqualsAndHashCode + @ToString + protected static class StubProxyContext { + private final T annotation; + private final Class stubType; + + private StubProxyContext(@NonNull Class stubType, T annotation) { + this.annotation = annotation; + this.stubType = stubType; + } + + public static StubProxyContext valueOf(Class stubType, T annotation) { + return new StubProxyContext<>(stubType, annotation); + } + } } diff --git a/src/main/java/com/github/yungyu16/spring/stub/proxy/DefaultStubProxyFactory.java b/src/main/java/com/github/yungyu16/spring/stub/proxy/DefaultStubProxyFactory.java new file mode 100644 index 0000000..aed4f62 --- /dev/null +++ b/src/main/java/com/github/yungyu16/spring/stub/proxy/DefaultStubProxyFactory.java @@ -0,0 +1,81 @@ +package com.github.yungyu16.spring.stub.proxy; + +import com.github.yungyu16.spring.stub.annotation.ProxyStub; +import lombok.NonNull; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * CreatedDate: 2020/11/25 + * Author: songjialin + */ +public class DefaultStubProxyFactory implements StubProxyFactory, BeanFactoryAware { + + private BeanFactory beanFactory; + + @SuppressWarnings("rawtypes,unchecked") + @Override + public T createProxy(Class stubInterface, ProxyStub stubAnnotation) { + AbstractInvocationDispatcher invocationDispatcher = getInvocationDispatcher(stubInterface, stubAnnotation); + Class annotationType = invocationDispatcher.getAnnotationType(); + Annotation annotation = AnnotationUtils.findAnnotation(stubInterface, annotationType); + AbstractInvocationDispatcher.StubProxyContext stubProxyContext = AbstractInvocationDispatcher.StubProxyContext.valueOf(stubInterface, annotation); + return (T) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), collectProxyInterface(stubInterface), StubInvocationHandler.newInstance(stubProxyContext, invocationDispatcher)); + } + + @SuppressWarnings("rawtypes") + private AbstractInvocationDispatcher getInvocationDispatcher(@NonNull Class type, @NonNull ProxyStub proxyStub) { + Class dispatcherType = proxyStub.dispatcherType(); + Object handler; + if (dispatcherType != AbstractInvocationDispatcher.class) { + handler = beanFactory.getBean(dispatcherType); + } else { + throw new BeanCreationException(type.getName() + " 没有指定InvocationDispatcher"); + } + return (AbstractInvocationDispatcher) handler; + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } + + @SuppressWarnings("rawtypes") + static class StubInvocationHandler implements InvocationHandler { + + private final AbstractInvocationDispatcher.StubProxyContext stubProxyContext; + private final AbstractInvocationDispatcher dispatcher; + + private StubInvocationHandler(AbstractInvocationDispatcher.StubProxyContext stubProxyContext, AbstractInvocationDispatcher dispatcher) { + this.stubProxyContext = stubProxyContext; + this.dispatcher = dispatcher; + } + + public static StubInvocationHandler newInstance(@NonNull AbstractInvocationDispatcher.StubProxyContext stubProxyContext, @NonNull AbstractInvocationDispatcher dispatcher) { + return new StubInvocationHandler(stubProxyContext, dispatcher); + } + + @Override + @SuppressWarnings("unchecked") + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (ReflectionUtils.isToStringMethod(method)) { + return "ProxyStub:" + ClassUtils.classNamesToString(stubProxyContext.getStubType()) + ":" + stubProxyContext.getAnnotation(); + } + if (ReflectionUtils.isEqualsMethod(method) + || ReflectionUtils.isHashCodeMethod(method)) { + return method.invoke(this, args); + } + return dispatcher.invoke(stubProxyContext, proxy, method, args); + } + } +} diff --git a/src/main/java/com/github/yungyu16/spring/stub/proxy/StubProxyFactory.java b/src/main/java/com/github/yungyu16/spring/stub/proxy/StubProxyFactory.java new file mode 100644 index 0000000..1b6f069 --- /dev/null +++ b/src/main/java/com/github/yungyu16/spring/stub/proxy/StubProxyFactory.java @@ -0,0 +1,16 @@ +package com.github.yungyu16.spring.stub.proxy; + +import com.github.yungyu16.spring.stub.annotation.ProxyStub; + +/** + * CreatedDate: 2020/11/25 + * Author: songjialin + */ +public interface StubProxyFactory { + + default Class[] collectProxyInterface(Class interfaceType) { + return new Class[]{interfaceType, StubProxyLabel.class}; + } + + T createProxy(Class stubInterface, ProxyStub stubAnnotation); +} diff --git a/src/main/java/com/github/yungyu16/spring/proxy/StubLabel.java b/src/main/java/com/github/yungyu16/spring/stub/proxy/StubProxyLabel.java similarity index 52% rename from src/main/java/com/github/yungyu16/spring/proxy/StubLabel.java rename to src/main/java/com/github/yungyu16/spring/stub/proxy/StubProxyLabel.java index 74c1329..2d0a0a7 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/StubLabel.java +++ b/src/main/java/com/github/yungyu16/spring/stub/proxy/StubProxyLabel.java @@ -1,9 +1,9 @@ -package com.github.yungyu16.spring.proxy; +package com.github.yungyu16.spring.stub.proxy; /** * 动态代理标记接口 * CreatedDate: 2020/11/24 * Author: songjialin */ -public interface StubLabel { +public interface StubProxyLabel { } diff --git a/src/main/java/com/github/yungyu16/spring/proxy/support/BeanDefinitionRegistryPostProcessorAdapter.java b/src/main/java/com/github/yungyu16/spring/stub/support/BeanDefinitionRegistryPostProcessorAdapter.java similarity index 93% rename from src/main/java/com/github/yungyu16/spring/proxy/support/BeanDefinitionRegistryPostProcessorAdapter.java rename to src/main/java/com/github/yungyu16/spring/stub/support/BeanDefinitionRegistryPostProcessorAdapter.java index ce9fa5a..1009fe6 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/support/BeanDefinitionRegistryPostProcessorAdapter.java +++ b/src/main/java/com/github/yungyu16/spring/stub/support/BeanDefinitionRegistryPostProcessorAdapter.java @@ -1,4 +1,4 @@ -package com.github.yungyu16.spring.proxy.support; +package com.github.yungyu16.spring.stub.support; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; diff --git a/src/main/java/com/github/yungyu16/spring/proxy/support/ClassPathStubBeanDefinitionScanner.java b/src/main/java/com/github/yungyu16/spring/stub/support/ClassPathStubBeanDefinitionScanner.java similarity index 90% rename from src/main/java/com/github/yungyu16/spring/proxy/support/ClassPathStubBeanDefinitionScanner.java rename to src/main/java/com/github/yungyu16/spring/stub/support/ClassPathStubBeanDefinitionScanner.java index b3dad16..13afc0c 100644 --- a/src/main/java/com/github/yungyu16/spring/proxy/support/ClassPathStubBeanDefinitionScanner.java +++ b/src/main/java/com/github/yungyu16/spring/stub/support/ClassPathStubBeanDefinitionScanner.java @@ -1,6 +1,6 @@ -package com.github.yungyu16.spring.proxy.support; +package com.github.yungyu16.spring.stub.support; -import com.github.yungyu16.spring.proxy.annotation.ProxyStub; +import com.github.yungyu16.spring.stub.annotation.ProxyStub; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index e3a0b24..90d5078 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,2 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.github.yungyu16.spring.proxy.StubBeanAutoConfiguration +com.github.yungyu16.spring.stub.StubBeanAutoConfiguration diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/HelloServiceTest.java b/src/test/java/com/github/yungyu16/spring/proxy/example/HelloServiceTest.java deleted file mode 100644 index 2279e61..0000000 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/HelloServiceTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.yungyu16.spring.proxy.example; - -import com.github.yungyu16.spring.proxy.example.service.HelloService1; -import com.github.yungyu16.spring.proxy.example.service.HelloService2; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -/** - * CreatedDate: 2020/11/24 - * Author: songjialin - */ -@RunWith(SpringRunner.class) -@SpringBootTest -public class HelloServiceTest { - @Autowired - private HelloService1 helloService1; - @Autowired - private HelloService2 helloService2; - - @Test - public void testHello1() { - helloService1.hello(); - helloService2.hello(); - } -} diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/service/HelloService1.java b/src/test/java/com/github/yungyu16/spring/proxy/example/service/HelloService1.java deleted file mode 100644 index c5a27a2..0000000 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/service/HelloService1.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.github.yungyu16.spring.proxy.example.service; - - -import com.github.yungyu16.spring.proxy.example.annotation.TestClient; - -/** - * CreatedDate: 2020/11/24 - * Author: songjialin - */ -@TestClient("helloService") -public interface HelloService1 { - void hello(); -} diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/service/InvocationDispatcherImpl1.java b/src/test/java/com/github/yungyu16/spring/proxy/example/service/InvocationDispatcherImpl1.java deleted file mode 100644 index cdef83e..0000000 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/service/InvocationDispatcherImpl1.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.yungyu16.spring.proxy.example.service; - -import com.github.yungyu16.spring.proxy.AbstractInvocationDispatcher; -import com.github.yungyu16.spring.proxy.StubContext; -import com.github.yungyu16.spring.proxy.example.annotation.TestClient; -import org.springframework.stereotype.Component; - -import java.lang.reflect.Method; - -/** - * CreatedDate: 2020/11/24 - * Author: songjialin - */ -@Component -public class InvocationDispatcherImpl1 extends AbstractInvocationDispatcher { - @Override - protected Object invoke(StubContext stubContext, Object proxy, Method method, Object[] args) throws Throwable { - System.out.println(stubContext.getAnnotation()); - System.out.println("InvocationDispatcherImpl1"); - return null; - } -} diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/service/InvocationDispatcherImpl2.java b/src/test/java/com/github/yungyu16/spring/proxy/example/service/InvocationDispatcherImpl2.java deleted file mode 100644 index ebe67f1..0000000 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/service/InvocationDispatcherImpl2.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.yungyu16.spring.proxy.example.service; - -import com.github.yungyu16.spring.proxy.AbstractInvocationDispatcher; -import com.github.yungyu16.spring.proxy.StubContext; -import com.github.yungyu16.spring.proxy.annotation.ProxyStub; -import org.springframework.stereotype.Component; - -import java.lang.reflect.Method; - -/** - * CreatedDate: 2020/11/24 - * Author: songjialin - */ -@Component -public class InvocationDispatcherImpl2 extends AbstractInvocationDispatcher { - @Override - protected Object invoke(StubContext stubContext, Object proxy, Method method, Object[] args) throws Throwable { - System.out.println(stubContext.getAnnotation()); - System.out.println("InvocationDispatcherImpl2"); - return null; - } -} diff --git a/src/test/java/com/github/yungyu16/spring/stub/example/HelloServiceTest.java b/src/test/java/com/github/yungyu16/spring/stub/example/HelloServiceTest.java new file mode 100644 index 0000000..2ded538 --- /dev/null +++ b/src/test/java/com/github/yungyu16/spring/stub/example/HelloServiceTest.java @@ -0,0 +1,36 @@ +package com.github.yungyu16.spring.stub.example; + +import com.github.yungyu16.spring.stub.example.service.HelloService1; +import com.github.yungyu16.spring.stub.example.service.HelloService2; +import com.github.yungyu16.spring.stub.example.service.HelloService3; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * CreatedDate: 2020/11/24 + * Author: songjialin + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class HelloServiceTest { + @Autowired + private HelloService1 helloService1; + @Autowired + private HelloService2 helloService2; + @Autowired + private HelloService3 helloService3; + + @Test + public void testHello1() { + String hello1 = helloService1.hello(); + String hello2 = helloService2.hello(); + String hello3 = helloService3.hello(); + Assert.assertSame("InvocationDispatcherImpl1", hello1); + Assert.assertSame("InvocationDispatcherImpl2", hello2); + Assert.assertSame("StubProxyFactory3", hello3); + } +} diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/TestApplication.java b/src/test/java/com/github/yungyu16/spring/stub/example/TestApplication.java similarity index 75% rename from src/test/java/com/github/yungyu16/spring/proxy/example/TestApplication.java rename to src/test/java/com/github/yungyu16/spring/stub/example/TestApplication.java index a77c920..5bbd838 100644 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/TestApplication.java +++ b/src/test/java/com/github/yungyu16/spring/stub/example/TestApplication.java @@ -1,6 +1,6 @@ -package com.github.yungyu16.spring.proxy.example; +package com.github.yungyu16.spring.stub.example; -import com.github.yungyu16.spring.proxy.annotation.ProxyStubScan; +import com.github.yungyu16.spring.stub.annotation.ProxyStubScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/annotation/TestClient.java b/src/test/java/com/github/yungyu16/spring/stub/example/annotation/TestClient.java similarity index 65% rename from src/test/java/com/github/yungyu16/spring/proxy/example/annotation/TestClient.java rename to src/test/java/com/github/yungyu16/spring/stub/example/annotation/TestClient.java index 2b8a464..73dfb0e 100644 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/annotation/TestClient.java +++ b/src/test/java/com/github/yungyu16/spring/stub/example/annotation/TestClient.java @@ -1,7 +1,7 @@ -package com.github.yungyu16.spring.proxy.example.annotation; +package com.github.yungyu16.spring.stub.example.annotation; -import com.github.yungyu16.spring.proxy.annotation.ProxyStub; -import com.github.yungyu16.spring.proxy.example.service.InvocationDispatcherImpl1; +import com.github.yungyu16.spring.stub.annotation.ProxyStub; +import com.github.yungyu16.spring.stub.example.service.InvocationDispatcherImpl1; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; diff --git a/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService1.java b/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService1.java new file mode 100644 index 0000000..6719084 --- /dev/null +++ b/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService1.java @@ -0,0 +1,13 @@ +package com.github.yungyu16.spring.stub.example.service; + + +import com.github.yungyu16.spring.stub.example.annotation.TestClient; + +/** + * CreatedDate: 2020/11/24 + * Author: songjialin + */ +@TestClient("helloService") +public interface HelloService1 { + String hello(); +} diff --git a/src/test/java/com/github/yungyu16/spring/proxy/example/service/HelloService2.java b/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService2.java similarity index 50% rename from src/test/java/com/github/yungyu16/spring/proxy/example/service/HelloService2.java rename to src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService2.java index 014ce2c..04cd784 100644 --- a/src/test/java/com/github/yungyu16/spring/proxy/example/service/HelloService2.java +++ b/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService2.java @@ -1,7 +1,7 @@ -package com.github.yungyu16.spring.proxy.example.service; +package com.github.yungyu16.spring.stub.example.service; -import com.github.yungyu16.spring.proxy.annotation.ProxyStub; +import com.github.yungyu16.spring.stub.annotation.ProxyStub; /** * CreatedDate: 2020/11/24 @@ -9,5 +9,5 @@ */ @ProxyStub(InvocationDispatcherImpl2.class) public interface HelloService2 { - void hello(); + String hello(); } diff --git a/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService3.java b/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService3.java new file mode 100644 index 0000000..90ebcd1 --- /dev/null +++ b/src/test/java/com/github/yungyu16/spring/stub/example/service/HelloService3.java @@ -0,0 +1,13 @@ +package com.github.yungyu16.spring.stub.example.service; + + +import com.github.yungyu16.spring.stub.annotation.ProxyStub; + +/** + * CreatedDate: 2020/11/24 + * Author: songjialin + */ +@ProxyStub(factoryType = StubProxyFactory3.class) +public interface HelloService3 { + String hello(); +} diff --git a/src/test/java/com/github/yungyu16/spring/stub/example/service/InvocationDispatcherImpl1.java b/src/test/java/com/github/yungyu16/spring/stub/example/service/InvocationDispatcherImpl1.java new file mode 100644 index 0000000..98266eb --- /dev/null +++ b/src/test/java/com/github/yungyu16/spring/stub/example/service/InvocationDispatcherImpl1.java @@ -0,0 +1,20 @@ +package com.github.yungyu16.spring.stub.example.service; + +import com.github.yungyu16.spring.stub.example.annotation.TestClient; +import com.github.yungyu16.spring.stub.proxy.AbstractInvocationDispatcher; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +/** + * CreatedDate: 2020/11/24 + * Author: songjialin + */ +@Component +public class InvocationDispatcherImpl1 extends AbstractInvocationDispatcher { + @Override + protected Object invoke(StubProxyContext stubProxyContext, Object proxy, Method method, Object[] args) throws Throwable { + System.out.println("InvocationDispatcherImpl1"); + return "InvocationDispatcherImpl1"; + } +} diff --git a/src/test/java/com/github/yungyu16/spring/stub/example/service/InvocationDispatcherImpl2.java b/src/test/java/com/github/yungyu16/spring/stub/example/service/InvocationDispatcherImpl2.java new file mode 100644 index 0000000..5c24aac --- /dev/null +++ b/src/test/java/com/github/yungyu16/spring/stub/example/service/InvocationDispatcherImpl2.java @@ -0,0 +1,20 @@ +package com.github.yungyu16.spring.stub.example.service; + +import com.github.yungyu16.spring.stub.annotation.ProxyStub; +import com.github.yungyu16.spring.stub.proxy.AbstractInvocationDispatcher; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +/** + * CreatedDate: 2020/11/24 + * Author: songjialin + */ +@Component +public class InvocationDispatcherImpl2 extends AbstractInvocationDispatcher { + @Override + protected Object invoke(StubProxyContext stubProxyContext, Object proxy, Method method, Object[] args) throws Throwable { + System.out.println("InvocationDispatcherImpl2"); + return "InvocationDispatcherImpl2"; + } +} diff --git a/src/test/java/com/github/yungyu16/spring/stub/example/service/StubProxyFactory3.java b/src/test/java/com/github/yungyu16/spring/stub/example/service/StubProxyFactory3.java new file mode 100644 index 0000000..545aca0 --- /dev/null +++ b/src/test/java/com/github/yungyu16/spring/stub/example/service/StubProxyFactory3.java @@ -0,0 +1,32 @@ +package com.github.yungyu16.spring.stub.example.service; + +import com.github.yungyu16.spring.stub.annotation.ProxyStub; +import com.github.yungyu16.spring.stub.proxy.StubProxyFactory; +import org.springframework.stereotype.Component; +import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.Proxy; + +/** + * CreatedDate: 2020/11/25 + * Author: songjialin + */ +@Component +public class StubProxyFactory3 implements StubProxyFactory { + @SuppressWarnings("unchecked") + @Override + public T createProxy(Class stubInterface, ProxyStub stubAnnotation) { + return (T) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), collectProxyInterface(stubInterface), (proxy, method, args) -> { + if (ReflectionUtils.isToStringMethod(method)) { + return "ProxyStub:" + ClassUtils.classNamesToString(stubInterface) + ":" + stubAnnotation; + } + if (ReflectionUtils.isEqualsMethod(method) + || ReflectionUtils.isHashCodeMethod(method)) { + return method.invoke(this, args); + } + System.out.println("StubProxyFactory3"); + return "StubProxyFactory3"; + }); + } +} diff --git a/src/test/java/com/github/yungyu16/spring/proxy/test/package-info.java b/src/test/java/com/github/yungyu16/spring/stub/test/package-info.java similarity index 61% rename from src/test/java/com/github/yungyu16/spring/proxy/test/package-info.java rename to src/test/java/com/github/yungyu16/spring/stub/test/package-info.java index de0a7c6..43b2d40 100644 --- a/src/test/java/com/github/yungyu16/spring/proxy/test/package-info.java +++ b/src/test/java/com/github/yungyu16/spring/stub/test/package-info.java @@ -2,4 +2,4 @@ * @author Yungyu * @description Created by Yungyu on 2020/11/24. */ -package com.github.yungyu16.spring.proxy.test; \ No newline at end of file +package com.github.yungyu16.spring.stub.test;