惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
B
Blog
小众软件
小众软件
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
I
InfoQ
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
H
Help Net Security
雷峰网
雷峰网
S
SegmentFault 最新的问题
V
Visual Studio Blog
爱范儿
爱范儿

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
BeanDefinitionRegistryPostProcessor扩展
祈雨的笔记 · 2019-12-28 · via 祈雨的笔记

通过BeanDefinitionRegistryPostProcessor扩展能实现额外的很多功能,例如实现将一个接口动态代理,并将该代理对象在spring容器初始化完成前注册到spring容器中。实现可以通过@Autowired等注释或其他方法从spring容器中获取该代理对象。

代理工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class HelloProxy implements InvocationHandler {

private Class<?> interfaceClass;

public Object bind(Class<?> cls) {
this.interfaceClass = cls;
return Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{interfaceClass}, this);
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
log.info("调用代理方法, method: {}", method.getName());
return "hello world";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import org.springframework.beans.factory.FactoryBean;

public class ProxyBeanFactory<T> implements FactoryBean<T> {

private Class<T> interfaceClass;

public Class<T> getInterfaceClass() {
return interfaceClass;
}

public void setInterfaceClass(Class<T> interfaceClass) {
this.interfaceClass = interfaceClass;
}

@Override
public T getObject() {
return (T) new HelloProxy().bind(interfaceClass);
}

@Override
public Class<?> getObjectType() {
return interfaceClass;
}

@Override
public boolean isSingleton() {

return true;
}

}

扫描注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import com.wakzz.autowire.proxy.ProxyBeanFactory;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import java.lang.annotation.Annotation;
import java.util.Set;

public class ClassPathReferenceScanner extends ClassPathBeanDefinitionScanner {

private BeanDefinitionRegistry registry;
private Class<? extends Annotation> annotationClass;

public ClassPathReferenceScanner(BeanDefinitionRegistry registry) {
super(registry, false);
this.registry = registry;
}

public void setAnnotationClass(Class<? extends Annotation> annotationClass) {
this.annotationClass = annotationClass;
}




public void registerFilters() {
if (this.annotationClass != null) {
addIncludeFilter(new AnnotationTypeFilter(this.annotationClass));
}


addExcludeFilter(new TypeFilter() {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
String className = metadataReader.getClassMetadata().getClassName();
return className.endsWith("package-info");
}
});
}




@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent();
}

@Override
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
processBeanDefinitions(beanDefinitions);
return beanDefinitions;
}




private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
for (BeanDefinitionHolder holder : beanDefinitions) {
String className = holder.getBeanDefinition().getBeanClassName();

BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(className);
GenericBeanDefinition proxyDefinition = (GenericBeanDefinition) builder.getRawBeanDefinition();
proxyDefinition.getPropertyValues().add("interfaceClass", className);
proxyDefinition.setBeanClass(ProxyBeanFactory.class);
proxyDefinition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
this.registry.registerBeanDefinition(holder.getBeanName(), proxyDefinition);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.annotation.Annotation;

public class ReferenceRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {

private String basePackage;

private Class<? extends Annotation> annotationClass;

private ApplicationContext applicationContext;

public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}

public void setAnnotationClass(Class<? extends Annotation> annotationClass) {
this.annotationClass = annotationClass;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
ClassPathReferenceScanner scanner = new ClassPathReferenceScanner(registry);
scanner.setAnnotationClass(this.annotationClass);
scanner.setResourceLoader(this.applicationContext);
scanner.registerFilters();
scanner.scan(basePackage);
}

@Override
public void afterPropertiesSet() {
}

@Override
public void setBeanName(String name) {
}
}

注入实现

1
2
3
4
5
6
7
8
9
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Reference {
}
1
2
3
4
5
6
7
8
import com.wakzz.autowire.Reference;

@Reference
public interface TestService {

String sayHello();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.wakzz.autowire.Reference;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ReferenceConfig {

@Bean
public BeanDefinitionRegistryPostProcessor referenceRegistryPostProcessor() {
ReferenceRegistryPostProcessor referenceRegistryPostProcessor = new ReferenceRegistryPostProcessor();
referenceRegistryPostProcessor.setAnnotationClass(Reference.class);
referenceRegistryPostProcessor.setBasePackage("com.wakzz.autowire");
return referenceRegistryPostProcessor;
}

}

效果

1
2
3
4
5
6
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder()
.sources(Application.class)
.run(args);
TestService bean = applicationContext.getBean(TestService.class);
String response = bean.sayHello();
System.out.println(response);

最终输出如下:

1
2
2019-12-29 15:37:35.938  INFO 25336 --- [           main] com.wakzz.autowire.proxy.HelloProxy      : 调用代理方法, method: sayHello
hello world