[英]Create my own Annotation Java
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. 这是我第一次创建Annotation Java,并且想创建自己的注释,然后在必要时禁止执行测试。 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.
问题是我很多测试都必须使用Facebook api,有时它们无法正常工作,因此我想要一个名为@Facebook的注释,将其添加到测试中时可以用作@Suppress注释,因此编写了以下代码。 。不幸的是,它不起作用。 Anyone can help me?
有人可以帮助我吗?
@Retention(RetentionPolicy.RUNTIME)
public @interface Facebook {
Suppress notToBeRun() default @Suppress;
}
Java contains a flexible annotation API with numerous application possibilities. Java包含灵活的注释API,具有多种应用程序可能性。 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.
注释接口最初开发用于在Java EE堆栈中指定企业语义(Java Bean是无状态的还是有状态的,单例的,等等),现在,注释接口也已在Java中普遍用于上下文依赖注入(CDI)。 Your question addresses how to use the Java annotation API for CDI.
您的问题解决了如何将Java注解API用于CDI。
First, you need to define a qualifier interface-class for each specific user-defined CDI-option you want Java to Inject. 首先,您需要为要Java注入的每个特定用户定义的CDI选项定义一个限定符接口类。 You want a Facebook-implementation to be loaded by injection.
您希望通过注入方式来加载Facebook实现。 Your interface ( Facebook.java ) can look as follows:
您的界面( Facebook.java )如下所示:
@Qualifier
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Facebook {
}
The first term, @Qualifier
indicates that you define a new qualifier, actually a unique name ( @Facebook
) known by the Java injection mechanism. 第一个术语
@Qualifier
表示您定义了一个新的限定符,实际上是Java注入机制已知的唯一名称( @Facebook
)。
The @Target
annotation indicates that your annotation can precede a Java type declaration, a Java field (specifically a variable declaration) or a method parameter. @Target
注释表示您的注释可以位于Java类型声明,Java字段(特别是变量声明)或方法参数之前。 You can add a fourth qualifier to allow your annotation also to be used before a method, namely ElementType.METHOD
. 您可以添加第四个限定符,以允许在方法(即
ElementType.METHOD
之前使用注释。
@Documented
defines an annotation that assures that classes using this annotation show this in their generated JavaDoc. @Documented
定义一个注释,以确保使用该注释的类在其生成的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). 必须将
@Retention
设置为RetentionPolicy.RUNTIME
,以便在启动Java应用程序(在Web应用程序服务器上下文中部署)时使注释变为活动状态。
You now need to define a general Java interface class ( SocialMediaService.java ), just a plain Java interface: 现在,您需要定义一个通用的Java接口类( SocialMediaService.java ),只是一个普通的Java接口:
public interface SocialMediaService {
boolean login(String userId, String password);
void logout();
String searchForMessages(String[] friends);
}
This interface can be implemented in different ways, by means of the implements
Java-construct. 该接口可以通过
implements
Java构造以不同的方式implements
。 Using the previously defined annotation, you can choose in the Java-code which of the alternative implementations to use. 使用先前定义的注释,您可以在Java代码中选择使用哪个替代实现。
Here is the Facebook-example of a Java-class ( Facebook.java , in a different package than the interface qualifier class, specified above): 这是Java类的Facebook示例(与上面指定的接口限定符类不同的包中的Facebook.java ):
@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;
}
}
You can choose among numerous different implementations @LinkedIn
, etc. each with their specific Java implementation class (alternatives to public class Facebook
). 您可以在众多不同的实现
@LinkedIn
等中进行选择,每种实现@LinkedIn
其特定的Java实现类( public class Facebook
替代public class Facebook
)。
In your Java-class you are now ready to use CDI to inject the Java implementation of choice. 现在,您可以在Java类中使用CDI注入所选的Java实现。
Back-end Java-bean ( BackendSocialMediaAnalysis.java ) where CDI is being applied: 应用了CDI的后端Java Bean( BackendSocialMediaAnalysis.java ):
public class BackendSocialMediaAnalysis {
...
@Inject @Facebook
private SocialMediaService genericMediaService;
...
}
Replacing @Facebook
by @LinkedIn
results in the alternative (LinkedIn) implementation being loaded into genericMediaService. 用
@Facebook
替换@Facebook
@LinkedIn
导致将替代(LinkedIn)实现加载到genericMediaService中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.