org.springframework.cglib.proxy.InvocationHandler Java Examples

The following examples show how to use org.springframework.cglib.proxy.InvocationHandler. 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);
        }
    }
}