创建我自己的注释 Java [英] Create my own Annotation Java

查看:25
本文介绍了创建我自己的注释 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次创建注释 Java,我想创建自己的注释,然后在必要时禁止执行测试.问题是我的许多测试都必须使用 Facebook api,有时它们不起作用,所以我想要一个名为 @Facebook 的注释,当添加到测试中时,它作为 @Suppress 注释工作,所以我编写了以下代码...不幸的是不起作用.任何人都可以帮助我吗?

This is the first time I create an Annotation Java and I'd like to create a my own annotation then suppress the execution for a test when necessary. The problem is that I many of my tests I have to use Facebook api, sometimes they don't work so I want an annotation called @Facebook that when added to a test works as @Suppress annotation, so I wrote the following code...that unfortunally doesn't work. Anyone can help me?

        @Retention(RetentionPolicy.RUNTIME)
        public @interface Facebook {
            Suppress notToBeRun() default @Suppress;
        }

推荐答案

Java 包含一个灵活的注释 API,具有多种应用可能性.最初开发用于在 Java EE 堆栈中指定企业语义(Java-bean 是无状态的还是有状态的、单例等),现在注解接口也广泛用于 Java 中的上下文相关注入 (CDI).您的问题涉及如何将 Java 注释 API 用于 CDI.

Java contains a flexible annotation API with numerous application possibilities. First developed to specify enterprise semantics in the Java EE stack (whether a Java-bean is stateless or statefull, singleton, etc.), the annotation interface has now also found common use for Context Dependent Injection (CDI) in Java. Your question addresses how to use the Java annotation API for CDI.

首先,您需要为您希望 Java 注入的每个特定的用户定义 CDI 选项定义一个限定符接口类.您希望通过注入加载 Facebook 实现.您的界面 (Facebook.java) 可能如下所示:

First, you need to define a qualifier interface-class for each specific user-defined CDI-option you want Java to Inject. You want a Facebook-implementation to be loaded by injection. Your interface (Facebook.java) can look as follows:

@Qualifier
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Facebook {
}

第一项 @Qualifier 表示您定义了一个新的限定符,实际上是 Java 注入机制已知的唯一名称 (@Facebook).

The first term, @Qualifier indicates that you define a new qualifier, actually a unique name (@Facebook) known by the Java injection mechanism.

@Target 注释表明您的注释可以位于 Java 类型声明、Java 字段(特别是变量声明)或方法参数之前.您可以添加第四个限定符以允许您的注释也可以在方法之前使用,即 ElementType.METHOD.

The @Target annotation indicates that your annotation can precede a Java type declaration, a Java field (specifically a variable declaration) or a method parameter. You can add a fourth qualifier to allow your annotation also to be used before a method, namely ElementType.METHOD.

@Documented 定义了一个注解,以确保使用此注解的类在其生成的 JavaDoc 中显示这一点.@Retention 必须设置为 RetentionPolicy.RUNTIME 以便在 Java 应用程序启动(部署,在 web应用服务器上下文).

@Documented defines an annotation that assures that classes using this annotation show this in their generated JavaDoc. @Retention must be set to RetentionPolicy.RUNTIME in order for the annotation to become active when the Java-application is started (deployed, in web application server context).

您现在需要定义一个通用的 Java 接口类 (SocialMediaService.java),只是一个普通的 Java 接口:

You now need to define a general Java interface class (SocialMediaService.java), just a plain Java interface:

public interface SocialMediaService {
   boolean login(String userId, String password);
   void logout();
   String searchForMessages(String[] friends);
}

该接口可以通过implements Java 结构以不同方式实现.使用之前定义的注解,你可以选择在 Java 代码中使用哪个替代实现.

This interface can be implemented in different ways, by means of the implements Java-construct. Using the previously defined annotation, you can choose in the Java-code which of the alternative implementations to use.

以下是 Java 类的 Facebook 示例(Facebook.java,在与上面指定的接口限定符类不同包中):

Here is the Facebook-example of a Java-class (Facebook.java, in a different package than the interface qualifier class, specified above):

@Facebook
public class Facebook implements SocialMediaService {

   @Override
   public boolean login(String userId, String password) {
       ...
       your application logic
       ...
       return true;
   }
   @Override
   public void logout() {
       ...
       your application logic
       ...
   }
   @Override
   public String searchForMessages(String[] friends) {
       ...
       your application logic
       ...
       return searchResult;
   }  
}

您可以在众多不同的实现中进行选择 @LinkedIn 等,每个实现都有其特定的 Java 实现类(替代 公共类 Facebook).

You can choose among numerous different implementations @LinkedIn, etc. each with their specific Java implementation class (alternatives to public class Facebook).

在您的 Java 类中,您现在可以使用 CDI 注入所选的 Java 实现.

In your Java-class you are now ready to use CDI to inject the Java implementation of choice.

应用 CDI 的后端 Java-bean (BackendSocialMediaAnalysis.java):

Back-end Java-bean (BackendSocialMediaAnalysis.java) where CDI is being applied:

public class BackendSocialMediaAnalysis {
   ...
   @Inject @Facebook
   private SocialMediaService genericMediaService;
   ...
}

@Facebook 替换为 @LinkedIn 会导致将替代 (LinkedIn) 实现加载到 genericMediaService 中.

Replacing @Facebook by @LinkedIn results in the alternative (LinkedIn) implementation being loaded into genericMediaService.

这篇关于创建我自己的注释 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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