创建动态代理对象bean,并动态注入到spring容器中

使用过Mybatis的同学,应该都知道,我们只需要编写mybatis对应的接口和mapper XML文件即可,并不需要手动编写mapper接口的实现。这里mybatis就用到了JDK动态代理,并且将生成的接口代理对象动态注入到Spring容器中。

这里涉及到几个问题。也许有同学会有疑问,我们直接编写好类,加入@Component等注解不是可以注入了吗?或者在配置类(@Configuration)中直接声明该Bean类型不也可以注入吗?

但具体到mybatis,这里我们用的是接口。由于spring实例化对象时,如果没有特殊情况,默认都是通过反射形式来实例化Bean。而接口是无法直接通过Class.forName方式进行实例化的。

第二个问题,如果手动声明Bean,其实也可以。但是会比较麻烦。因为我们还要手动创建代理对象,可能还需要给该对象的属性,比如(sqlSessionFactory,dataSource)设置对应的Bean实例。这些都会比较麻烦。况且Mapper接口可能会有很多个。

下面,我也写一个简单例子。用于说明如何将动态代理生成的接口实例,动态的注入到Spring容器中,并且能正常调用这2个接口里面的方法,获取调用结果。

整个代码结构如下:

创建动态代理对象bean,并动态注入到spring容器中

如图所示,我有2个接口CalculateService和TestService,这2个接口并没有对应的实现类。现在我们通过动态代理生成实例,然后注入到TestController中

首先是创建一个SpringBoot maven工程。

TestController源码

package com.company.controller;

import com.company.service.CalculateService;
import com.company.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private CalculateService calculateService;

    @RequestMapping("/test")
    public String getHello() {
        String testList = testService.getList("code123","name456");
        String calculateResult = calculateService.getResult("测试");
        return (testList + "," +calculateResult);
    }
}

handler包下的ServiceBeanDefinitionRegistry源码:

package com.company.handler;

import com.company.service.CalculateService;
import com.company.service.TestService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 用于Spring动态注入自定义接口
 * @author lichuang
 */
@Component
public class ServiceBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //这里我为了简单起见,直接写了2个固定接口,一般我们是通过反射获取需要代理的接口的clazz列表
        //比如判断包下面的类,或者通过某注解标注的类等等
        List<Class<?>> beanClazzs = Arrays.asList(TestService.class, CalculateService.class);
        for (Class beanClazz : beanClazzs) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClazz);
            GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();

            //在这里,我们可以给该对象的属性注入对应的实例。
            //比如mybatis,就在这里注入了dataSource和sqlSessionFactory,
            // 注意,如果采用definition.getPropertyValues()方式的话,
            // 类似definition.getPropertyValues().add("interfaceType", beanClazz);
            // 则要求在FactoryBean(本应用中即ServiceFactory)提供setter方法,否则会注入失败
            // 如果采用definition.getConstructorArgumentValues(),
            // 则FactoryBean中需要提供包含该属性的构造方法,否则会注入失败
            definition.getConstructorArgumentValues().addGenericArgumentValue(beanClazz);

            //注意,这里的BeanClass是生成Bean实例的工厂,不是Bean本身。
            // FactoryBean是一种特殊的Bean,其返回的对象不是指定类的一个实例,
            // 其返回的是该工厂Bean的getObject方法所返回的对象。
            definition.setBeanClass(ServiceFactory.class);

            //这里采用的是byType方式注入,类似的还有byName等
            definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
            registry.registerBeanDefinition(beanClazz.getSimpleName(), definition);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}

ServiceFactory源码:

package com.company.handler;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 接口实例工厂,这里主要是用于提供接口的实例对象
 * @author lichuang
 * @param <T>
 */
public class ServiceFactory<T> implements FactoryBean<T> {

    private Class<T> interfaceType;

    public ServiceFactory(Class<T> interfaceType) {
        this.interfaceType = interfaceType;
    }

    @Override
    public T getObject() throws Exception {
        //这里主要是创建接口对应的实例,便于注入到spring容器中
        InvocationHandler handler = new ServiceProxy<>(interfaceType);
        return (T) Proxy.newProxyInstance(interfaceType.getClassLoader(),
                new Class[] {interfaceType},handler);
    }

    @Override
    public Class<T> getObjectType() {
        return interfaceType;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

ServiceProxy源码

package com.company.handler;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 动态代理,需要注意的是,这里用到的是JDK自带的动态代理,代理对象只能是接口,不能是类
 * @author lichuang
 */

public class ServiceProxy<T> implements InvocationHandler {

    private Class<T> interfaceType;

    public ServiceProxy(Class<T> intefaceType) {
        this.interfaceType = interfaceType;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this,args);
        }
        System.out.println("调用前,参数:{}" + args);
        //这里可以得到参数数组和方法等,可以通过反射,注解等,进行结果集的处理
        //mybatis就是在这里获取参数和相关注解,然后根据返回值类型,进行结果集的转换
        Object result = JSON.toJSONString(args);
        System.out.println("调用后,结果:{}" + result);
        return result;
    }
}

另外2个接口源码:

package com.company.service;

public interface CalculateService {

    String getResult(String name);
}

TestService接口

package com.company.service;

public interface TestService {

    String getList(String code, String name);
}

我们DEBUG运行,可以看到程序正常运行,两个Service接口都已正常的注入到控制器中了,程序也能正常返回接口。

创建动态代理对象bean,并动态注入到spring容器中

创建动态代理对象bean,并动态注入到spring容器中