【发布时间】:2021-01-06 11:28:03
【问题描述】:
我正在尝试创建类似@Repository 的系统。
我有很多接口,例如:
@Client(uri = "http://example.com", username = "httpBasicUsername", password = "httpBasicPassword")
public interface RequestRepository {
@Request(method = Method.POST, uri = "/mono")
Mono<Response> ex1(Object body);
@Request(method = Method.POST, uri = "/flux")
Flux<Response> ex2(Object body);
}
现在,我正在使用这个函数创建 bean:
@Bean
public RequestRepository requestRepository(WebClient.Builder builder) {
return (RequestRepository) Proxy.newProxyInstance(
RequestRepository.class.getClassLoader(),
new Class[]{RequestRepository.class},
new MyDynamicInvocationHandler(builder)
);
}
但是我有很多这样的接口。对于每个新接口,我都需要创建另一个 bean 函数。但我不想那样做。
如果有@Client注解,有没有办法说spring(spring boot)然后像这样创建bean等?
【问题讨论】:
-
BeanFactoryPostProcessor接口可用于根据某些条件动态创建 bean。你可以在你的情况下使用它。为方便起见,我建议将所有类放在同一个包中。 -
首先谢谢你。我必须创建自定义接口扫描仪并在其中注册我的代理。我是新的所有这些 bean 和代理的东西,所以再次感谢。
标签: java spring spring-boot