Mybatis是一个优秀的ORM框架,它支持定制化 SQL、存储过程以及高级映射,对Mybatis不熟悉的可以查看我的这篇文章:Mybatis原理,在使用中,我们往往会很惊讶,为啥我只定义了一个接口,就可以进行依赖注入,而且还能对数据库进行操作,这其实是基于代理模式
来实现的,对动态代理不了解的可以查看这篇文章:Java代理模式
本文将介绍如何实现和Mybatis一样,在SpringBoot启动的时候自动为所有接口创建代理实现类
这个包主要提供注册代理实现类的一些核心类
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.gjinggroupId>
<artifactId>proxy-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>commonartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>commonname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
/**
* @author Gjing
**/
public interface BaseService {
void ok();
}
/**
* @author Gjing
**/
class DefaultService implements BaseService {
@Override
public void ok() {
System.out.println("ok");
}
}
/**
* @author Gjing
**/
class ServiceProxy<T> implements InvocationHandler {
private Class<T> interfaces;
ServiceProxy(Class<T> interfaces) {
this.interfaces = interfaces;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getDeclaringClass().equals(interfaces)) {
System.out.println("执行您的方法:" + method.getName());
return method.getName();
} else {
return method.invoke(new DefaultService(), args);
}
}
}
/**
* @author Gjing
**/
class ServiceProxyFactoryBean<T> implements FactoryBean<T> {
private Class<T> interfaces;
public ServiceProxyFactoryBean(Class<T> interfaces) {
this.interfaces = interfaces;
}
@Override
@SuppressWarnings("unchecked")
public T getObject() throws Exception {
return (T) Proxy.newProxyInstance(interfaces.getClassLoader(), new Class[]{interfaces},
new ServiceProxy<>(interfaces));
}
@Override
public Class<?> getObjectType() {
return interfaces;
}
@Override
public boolean isSingleton() {
return true;
}
}
/**
* @author Gjing
**/
class ServiceInterfacesScanner extends ClassPathBeanDefinitionScanner {
ServiceInterfacesScanner(BeanDefinitionRegistry registry) {
//false表示不使用ClassPathBeanDefinitionScanner默认的TypeFilter
super(registry, false);
}
@Override
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
this.addFilter();
Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);
if (beanDefinitionHolders.isEmpty()) {
throw new NullPointerException("No interfaces");
}
this.createBeanDefinition(beanDefinitionHolders);
return beanDefinitionHolders;
}
/**
* 只扫描顶级接口
* @param beanDefinition bean定义
* @return boolean
*/
@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
AnnotationMetadata metadata = beanDefinition.getMetadata();
String[] interfaceNames = metadata.getInterfaceNames();
return metadata.isInterface() && metadata.isIndependent()&& Arrays.asList(interfaceNames).contains(BaseService.class.getName());
}
/**
* 扫描所有类
*/
private void addFilter() {
addIncludeFilter((metadataReader, metadataReaderFactory) -> true);
}
/**
* 为扫描到的接口创建代理对象
*
* @param beanDefinitionHolders beanDefinitionHolders
*/
private void createBeanDefinition(Set<BeanDefinitionHolder> beanDefinitionHolders) {
for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
GenericBeanDefinition beanDefinition = ((GenericBeanDefinition) beanDefinitionHolder.getBeanDefinition());
//将bean的真实类型改变为FactoryBean
beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition.getBeanClassName());
beanDefinition.setBeanClass(ServiceProxyFactoryBean.class);
beanDefinition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
}
}
}
/**
* @author Gjing
**/
public class ProxyRegister implements BeanDefinitionRegistryPostProcessor {
private String basePackage;
public ProxyRegister(String basePackage) {
this.basePackage = basePackage;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (ParamUtil.isEmpty(basePackage)) {
return;
}
ServiceInterfacesScanner scanner = new ServiceInterfacesScanner(registry);
scanner.doScan(basePackage);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
整个核心包就完成了,接下来定义一个普通项目并使用它
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.gjinggroupId>
<artifactId>commonartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
/**
* @author Gjing
*/
public interface UserService extends BaseService {
String getMethodName();
}
/**
* @author Gjing
**/
@RestController
public class TestController {
@Resource
private UserService userService;
@GetMapping("/test")
public void test() {
userService.ok();
userService.getMethodName();
}
}
/**
* @author Gjing
**/
@Configuration
public class DemoConfiguration {
@Bean
public ProxyRegister proxyRegister() {
return new ProxyRegister("com.example.demo.service");
}
}
本文到此就结束啦,如果文章中有任何错误或者疑问,可以在评论区留言,我会及时回复,本文源代码地址:proxy-demo