简体   繁体   中英

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. 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 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.

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 {
}

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

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 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).

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);
}

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.

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;
   }  
}

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM