java - How can I disable Java EE interceptor on application start up, for example by condition from database logger=info?
问题描述
I use Java EE and WebLogic server. I declare interceptor with annotated style: @Interceptor. I need to add some functionality which would disable certain interceptor. I use annotation to mark methods when Interceptor needs to invoke.
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Traced {
}
@Traced
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class MethodInvocationInterceptor implements Serializable {
private static final Logger LOG = Logger.getLogger(MethodInvocationInterceptor.class);
@AroundInvoke
public Object intercept(final InvocationContext ctx) throws Exception {
LOG.trace(ctx.getMethod().getName() + " method started");
return ctx.proceed();
}
解决方案
我能想到三种合理的方法。
首先与@Nikos 建议的相同,始终启用拦截器并根据配置添加条件逻辑。
第二种方法需要您监视AfterTypeDiscovery
event的 CDI 扩展。这使您可以获取备选方案、拦截器和装饰器的列表。这些列表是可变的,一旦您更改它们,容器必须考虑更改值。移除拦截器应该会导致拦截器被禁用。
这种方法再次使用 CDI 扩展,要求您使用事件监视AnnotatedType
拦截器ProcessAnnotatedType
并使用方法禁用它veto()
。或者,如果否决没有削减它,您可以更改AnnotatedType
并删除/替换拦截器绑定(尽管这种方法听起来有点“hacky”,因为您以没有绑定的拦截器结束)。
推荐阅读
- python - 如何使用 Python 代码通过套接字接收 SIP 消息?
- c# - .NET5 - @HiddenFor(model => model.RowVersion) 缓存值?
- vba - 邮件合并上的 VBA - 添加空白页?
- protractor - 错误:在 VS 代码上运行 e2e 测试套件时,EADDRINUSE 连接 EADDRINUSE 127.0.0.1:50596
- java - 具有混合类型(长和字符串)的哈希图时,类 java.lang.Long 无法转换为类 java.lang.String 错误
- c# - How do I remove objects inside the blast radius?
- flutter - flutter blue reading characterstics error
- docker - Push locally built docker images to Digital Ocean Container Registry
- javascript - v-if span not displaying when errors are filled vuejs
- javascript - How would I update the component that renders? Reactjs