类 AnnotationLiteral 的独立版本? [英] Standalone version of class AnnotationLiteral?

查看:26
本文介绍了类 AnnotationLiteral 的独立版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CDI(上下文依赖注入)框架包含很棒的类 javax.enterprise.util.AnnotationLiteral 使创建注解的实例变得非常容易(尤其对测试有用).

The CDI (Context Dependency Injection) Framework contains the awesome class javax.enterprise.util.AnnotationLiteral that makes it very easy to create an instance of an Annotation (especaly useful for tests).

我想在我自己的库中使用那个 util 类.但是因为我的库与 CDI 无关,所以我不想拥有所有其他的 CDI 东西.幸运的是 AnnotationLiteral 不使用标准 java 类之外的任何其他类,因此这通常应该是可能的.

I want to use that util class in my own library. But because my library has noting to do with CDI, I do not want to have all the other CDI stuff. Fortunately AnnotationLiteral does not use any other classes than standard java classes, so this should be possible in general.

到目前为止,包含 AnnotationLiteral 的最小库是官方的 CDI-API

Up to now the smallest Library that contains AnnotationLiteral is the official CDI-API

<dependency>
   <groupId>javax.enterprise</groupId>
   <artifactId>cdi-api</artifactId>
   <version>1.0</version>
</dependency>

但即使我切断了所有的依赖(通过 maven 排除),jar 也会包含很多其他 CDI-API 和 -SPI 的东西.

But even if I cut off all the dependencies (by maven exclude), the jar will contain a lot of other CDI-API and -SPI stuff.

所以我的问题是:是否已经有一些(或多或少官方的)(maven)库只包含 util.AnnotationLiteral 类,或者至少没有那么多其他的东西?

So my question is: Is there already some (more or less official) (maven) library that contains only the util.AnnotationLiteral Class, or at least not so much other stuff?

推荐答案

AnnotationLiteral 的 Apache 许可版本可以在 geronimo api.它看起来与 CDI 的其他部分没有任何依赖关系.如果您不需要它在您的测试中提供的 equals 和 hashCode 实现,您甚至可以直接实现一个注释接口.这不是很好的风格,但绝对有可能.CDI 需要额外的 equals 和 hashCode 实现来实现其限定符逻辑.下面是一个使用 jsf ManagedBean 注释的示例:

An Apache licensed version of AnnotationLiteral can be found in the geronimo api. It does not look like it has any dependencies to other parts of CDI. If you don't need the equals and hashCode implementation it provides in your tests you could even directly implement an annotation interface. This is not really good style but definitely possible. CDI needs the additional equals and hashCode implementation for its Qualifier logic. Here is an example using the jsf ManagedBean annotation:

public static ManagedBean test = new ManagedBean() {
    @Override
    public String name() {
        return "test";
    }

    @Override
    public boolean eager() {
        return false;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return ManagedBean.class;
    }  
};

这篇关于类 AnnotationLiteral 的独立版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆