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

查看:132
本文介绍了创建我自己的注释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是无状态还是statefull,singleton等),注释接口现在也已经在Java中发现了上下文依赖注入(CDI)的常见用途。您的问题涉及如何使用CDI的Java注释API。

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.

首先,您需要为每个特定用户定义的CDI选项定义限定符接口类Java注入您需要通过注入来加载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);
}

该界面可以通过不同的方式实现,通过实现 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.

这是Facebook-例如,上面指定的界面限定符类别的 Java类( 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实现类( public class 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.

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

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天全站免登陆