属于外部依赖性的类方法的拦截器

发布于 2025-02-13 04:38:11 字数 558 浏览 0 评论 0 原文

我们的代码中有一个方面,它在休眠类别上一直是尖锐的。

我们的外观类看起来像这样:

@PointCut("(execution(* *.getQueryString(..))" + "|| execution(* *.getQuery(..)))" + "&& (target(org.hibernate.engine.NamedSQLQueryDefinition))")
public void aroundNamedSQLQueryDefinitionGetQuery() {
}

@Around("aroundNamedSQLQueryDefinitionGetQuery")
public String addExtraFilter(ProceedingJoinPoint pjp) throws Exception {
    //Logic to add extra filter to the Query.
}

现在我们正在尝试将此代码迁移到Quarkus。我们已经用属于我们模块的代码上的拦截器代替了方面。

但是,我们如何在休眠课上添加拦截器? 有其他方法可以实现这一目标吗?

We have an Aspect in our code that had been PointCut on Hibernate class.

Our Aspect class looks something like this:

@PointCut("(execution(* *.getQueryString(..))" + "|| execution(* *.getQuery(..)))" + "&& (target(org.hibernate.engine.NamedSQLQueryDefinition))")
public void aroundNamedSQLQueryDefinitionGetQuery() {
}

@Around("aroundNamedSQLQueryDefinitionGetQuery")
public String addExtraFilter(ProceedingJoinPoint pjp) throws Exception {
    //Logic to add extra filter to the Query.
}

Now we are trying to migrate this code to Quarkus. We have replaced Aspects with Interceptors which were present on code belonging to our modules.

But how do we add Interceptors on Hibernate classes?
Is there an alternate way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2025-02-20 04:38:11

a quarkus伸展允许您操纵冬眠类(或您自己的类)。

为了踩踏基本扩展名,

mvn io.quarkus.platform:quarkus-maven-plugin:2.10.1.Final:create-extension -N \
    -DgroupId=org.you \ 
    -DextensionId=aspectorama

然后,在 [whything]处理器的类中,您可以添加 andationTransFormerBuildItem

    @BuildStep
    AnnotationsTransformerBuildItem transform() {
        return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

            public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) {
                return kind == org.jboss.jandex.AnnotationTarget.Kind.METHOD;
            }

            public void transform(TransformationContext context) {
              if ("org.hibernate.engine.NamedSQLQueryDefinition".equals(context.getTarget().asMethod().declaringClass().name()) && ("getQueryString").equals(context.getTarget().asMethod().name()) {
                    context.transform().add(YourAnnotation.class).done();
                }
            }
        });
    }

(我尚未测试过,我可能没有您预定的方法。我只做 getquerystring 不是 getQuery ...但是它显示了这个想法。)

您可能还需要告诉Quarkus interceptor是否不在主要应用程序代码库中:

   /**
     * Makes the interceptor as a bean so we can access it.
     */
    @BuildStep
    void beans(BuildProducer<AdditionalBeanBuildItem> producer) {
        producer.produce(AdditionalBeanBuildItem.unremovableOf(YourInterceptor.class));
        producer.produce(AdditionalBeanBuildItem.unremovableOf(OtherExtraBean.class));
    }

可能是注释路线不是最适合您的用例的路线,您可以更直接地进行更改。值得浏览所有的quarkus构建项目,就像是一个内置的库扩展功能。例如,您可以将 @record 用于< bytecode 。

A Quarkus extension would allow you to manipulate the Hibernate classes (or your own classes).

To scaffold a basic extension,

mvn io.quarkus.platform:quarkus-maven-plugin:2.10.1.Final:create-extension -N \
    -DgroupId=org.you \ 
    -DextensionId=aspectorama

Then, in the [whatever]Processor class that gets created, you could add an AnnotationTransformerBuildItem

    @BuildStep
    AnnotationsTransformerBuildItem transform() {
        return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

            public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) {
                return kind == org.jboss.jandex.AnnotationTarget.Kind.METHOD;
            }

            public void transform(TransformationContext context) {
              if ("org.hibernate.engine.NamedSQLQueryDefinition".equals(context.getTarget().asMethod().declaringClass().name()) && ("getQueryString").equals(context.getTarget().asMethod().name()) {
                    context.transform().add(YourAnnotation.class).done();
                }
            }
        });
    }

(I haven't tested that, and I may not have quite the method names you intended. I only did getQueryString not getQuery... but it shows the idea.)

You may also need to tell Quarkus about your interceptor if it's not in the main application codebase:

   /**
     * Makes the interceptor as a bean so we can access it.
     */
    @BuildStep
    void beans(BuildProducer<AdditionalBeanBuildItem> producer) {
        producer.produce(AdditionalBeanBuildItem.unremovableOf(YourInterceptor.class));
        producer.produce(AdditionalBeanBuildItem.unremovableOf(OtherExtraBean.class));
    }

It may be that the annotation route isn't the best for your use case, and you could make the changes you needed more directly. It's worth browsing all the Quarkus build items, which are kind of like a library of built-in extension capabilities. For example, you can use @Record to create bytecode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文