org.springframework.cglib.proxy.Proxy Java Examples

The following examples show how to use org.springframework.cglib.proxy.Proxy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpcAutoConfiguration.java    From spring-boot-starter-grpc with MIT License 6 votes vote down vote up
static void registerBeans(BeanFactory beanFactory, Set<BeanDefinition> beanDefinitions) {
    for (BeanDefinition beanDefinition : beanDefinitions) {
        String className = beanDefinition.getBeanClassName();
        if (StringUtils.isEmpty(className)) {
            continue;
        }
        try {
            // 创建代理类
            Class<?> target = Class.forName(className);
            Object invoker = new Object();
            InvocationHandler invocationHandler = new GrpcServiceProxy<>(target, invoker);
            Object proxy = Proxy.newProxyInstance(GrpcService.class.getClassLoader(), new Class[]{target}, invocationHandler);

            // 注册到 Spring 容器
            String beanName = ClassNameUtils.beanName(className);
            ((DefaultListableBeanFactory) beanFactory).registerSingleton(beanName, proxy);
        } catch (ClassNotFoundException e) {
            log.warn("class not found : " + className);
        }
    }
}
 
Example #2
Source File: ClientFactory.java    From faster-framework-project with Apache License 2.0 6 votes vote down vote up
public Object createClientProxy(Class<?> target) {
    GRpcService grpcService = target.getAnnotation(GRpcService.class);
    ChannelProperty channelProperty = serverChannelMap.get(grpcService.value());
    if (channelProperty == null) {
        throw new GRpcChannelCreateException("GRpcService scheme:{" + grpcService.value() + "} was not found in properties.Please check your configuration.");
    }
    ManageChannelProxy manageChannelProxy = new ManageChannelProxy(channelProperty, marshallerFactory);
    //获取该类下所有包含GrpcMethod的注解,创建call定义
    Map<Method, GRpcMethod> annotatedMethods = MethodIntrospector.selectMethods(target,
            (MethodIntrospector.MetadataLookup<GRpcMethod>) method -> AnnotatedElementUtils.findMergedAnnotation(method, GRpcMethod.class));
    annotatedMethods.forEach((k, v) -> {
        String annotationMethodName = v.value();
        MethodCallProperty methodCallProperty = new MethodCallProperty();
        methodCallProperty.setMethod(k);
        methodCallProperty.setMethodName(StringUtils.isEmpty(annotationMethodName) ? k.getName() : annotationMethodName);
        methodCallProperty.setMethodType(v.type());
        methodCallProperty.setScheme(grpcService.scheme());
        manageChannelProxy.addCall(methodCallProperty);
    });
    return Proxy.newProxyInstance(target.getClassLoader(), new Class[]{target}, manageChannelProxy);
}