有没有办法复合拦截器
Is there a way to composite Interceptors
我是 ByteBuddy 的新手,有一个简单的问题。有什么方法可以通过注释将拦截器组合在一起f.e。类似于:
@Logging
@Transactional
public void foo() {}
是否会添加日志记录拦截器并确保它是事务性的?
当然,如果你用的是ByteBuddy
实例,就看你用过的ElementMatcher
了:
annotatedWith(Logging.class).or(annotatedWith(Transactional.class))
当您使用 AgentBuilder
时,您将为每种类型定义一个工具,您可以使用
组合不同的类型匹配器
.type(declaresMethod(annotatedWith(Logging.class)))
.transform(new LoggingTranformer())
.asDecorator()
.type(declaresMethod(annotatedWith(Transactional.class)))
.transform(new TransactionalTranformer())
.asDecorator()
使用asDecorator()
命令,将两个变压器链接起来。
感谢 Rafael Winterhalter 和讨论,我发布了他的建议答案:
You would need to write your own generic dispatcher. I would suggest you to create some form of generic dispatcher which then delegate programmatically.
For example:
isAnnotatedWith(anyOf(dispatcher.getAnnotationTypes())
where the dispatcher then routes to the components in question.
我是 ByteBuddy 的新手,有一个简单的问题。有什么方法可以通过注释将拦截器组合在一起f.e。类似于:
@Logging
@Transactional
public void foo() {}
是否会添加日志记录拦截器并确保它是事务性的?
当然,如果你用的是ByteBuddy
实例,就看你用过的ElementMatcher
了:
annotatedWith(Logging.class).or(annotatedWith(Transactional.class))
当您使用 AgentBuilder
时,您将为每种类型定义一个工具,您可以使用
.type(declaresMethod(annotatedWith(Logging.class)))
.transform(new LoggingTranformer())
.asDecorator()
.type(declaresMethod(annotatedWith(Transactional.class)))
.transform(new TransactionalTranformer())
.asDecorator()
使用asDecorator()
命令,将两个变压器链接起来。
感谢 Rafael Winterhalter 和讨论,我发布了他的建议答案:
You would need to write your own generic dispatcher. I would suggest you to create some form of generic dispatcher which then delegate programmatically.
For example:isAnnotatedWith(anyOf(dispatcher.getAnnotationTypes())
where the dispatcher then routes to the components in question.