Java Annotations Catalog
While there is and can be no complete catalog of Java annotations since anybody can create their own,
here are some of the annotations commonly used in Java SE the Java Enterprise.
There is no attempt at completeness; for example, many of the
JAXB annotations
which are primarily used in generated code have been omitted (but their names all begin with Xml
so they’re easy to categorize).
Name | API | Meaning |
---|---|---|
@After |
JUnit4 |
Run instance method after each test |
@AfterAll |
JUnit5 |
method to run after all tests in the current test class. |
@AfterClass |
JUnit4 |
Run static method after all tests |
@AfterEach |
JUnit5 |
method to run after each @Test method in the current test class. |
@ApplicationPath |
JAX-RS |
Root path of REST calls |
@ApplicationScoped |
CDI/JSF |
Object scoped to entire application |
@Autowired |
Spring |
mark the dependency that Spring is going to resolve and inject |
@Basic |
JPA |
Specify column parameters |
@Bean |
Spring |
Marks method as bean provider (advanced topic) |
@Before |
JUnit4 |
Run before each test all tests |
@BeforeAll |
JUnit5 |
method to run before all tests in the current test class. |
@BeforeClass |
JUnit4 |
Run static method before all tests |
@BeforeEach |
JUnit5 |
method to run before each @Test method in the current test class. |
@Column |
JPA |
Override defaults such as column name |
@Component |
Spring |
Makes this object a Bean |
@Consumes |
JAX-RS |
Content-type to accept and to translate object from |
@Context |
||
@Controller |
SpringMVC |
Mark class as web handler |
@ConversationScoped |
||
@Convert |
JPA |
Specifies which converter to use |
@Converter |
JPA |
Defines class as a converter |
@Dependent |
CDI |
Object depends on another object |
@DependsOn |
Spring |
force Spring IoC to initialize one or more beans before this bean |
@Deprecated |
SE |
Compile-time - generate warnings for using obsolete or dangerous methods |
@Disabled |
JUnit5 |
test class or test method is currently disabled; will not be executed. |
@DiscriminatorColumn |
JPA |
For SINGLE_TABLE: name of type field |
@DiscriminatorValue |
JPA |
For SINGLE_TABLE: value of type field for this subclass |
@DisplayName |
JUnit5 |
declare a custom display name for the annotated test class or test method. |
@Documented |
SE |
Compile-time - make this type part of the documented public API |
@ElementCollection |
JPA |
|
BVS |
Field must be syntactically-correct email |
|
@Embeddable/@Embedded |
JPA |
Embedded object lives in same row of database |
@EmbeddedId |
JPA |
Compount Primary Key is embedded |
@End |
||
@Entity |
JPA |
Object is a JPA Entity |
@Enumerated |
JPA |
Property is a Java Annotation |
@FacesConverter |
JSF |
Object is a JSF converter |
@Field |
JPA |
|
@FormParam |
JAX-RS |
Name of form parameter to retrieve and pass into method |
@FunctionalInterface |
java.lang |
Interface is claimed to be functional (one non-default method) |
@GET |
JAX-RS |
REST method invoked with HTTP GET |
@Generated |
JA |
Used to indicated that class has been generated by code |
@GeneratedValue |
JPA |
How to generate primary key for new objects |
@HeaderParam |
JAX-RS |
Name of HTTP Header to retrieve and pass into method |
@Id |
JPA |
Property maps to primary key column |
@Ignore |
JUnit |
Ignore this test method |
@Import |
Spring |
Causes one @Configuration class to import another |
@ImportResource |
Spring |
load beans from an applicationContext.xml file into the bean factory |
@Index |
JPA |
Used in @Table &c to generate index on named column(s) |
@Indexed |
Spring |
|
@Inheritance |
JPA |
Type of inheritance strategy to use |
@Inherited |
SE |
Compile-time - annotation type is automatically inherited. |
@Inject |
JI |
Inject object at method/field/constructor |
@JoinColumn |
JPA |
Name of column used to join tables |
@Lazy |
Spring |
Mark Spring singleton bean to be lazily loaded |
@Lob |
JPA |
Object is a Large (binary) Object |
@Local |
EJB |
EJB is to be accessed locally (in-vm) |
@Lookup |
Spring |
Replace bean method with lookup of bean having same type as method return |
@ManagedBean |
JI/JSF |
Object can be referred to by name |
@ManagedProperty |
JSF |
Property is to be injected |
@ManyToMany |
JPA |
Relationship |
@ManyToOne |
JPA |
Relationship |
@MapKey |
||
@Min @Max |
BVS |
Property must be within range to pass validation |
@ModelAttribute |
SpringMVC |
Command Object parameter to method is in ModelMap |
@Mojo |
Maven |
|
@Named |
CDI |
Used to disambiguate CDI injections |
@NamedQuery/NamedQueries |
JPA |
Externalized JPA Query |
@Nested |
JUnit5 |
nested non-static test class. |
@NotEmpty |
BVS |
Field must be non-empty (String or collection) |
@NotNull |
BVS |
Element may not be null |
@OneToMany |
JPA |
Relationship is one of this Entity to many of those in field. |
@OneToOne |
JPA |
Specify relationship ordinality |
@OrderBy |
JPA |
Specify sort order on a relationship |
@OrderColumn |
JPA |
|
@Override |
SE |
Compile-time check that method is a valid override |
@Override |
java.lang |
Compile-time: ensure method is override not overload |
@POST |
JAX-RS |
REST method invoked with specified HTTP method |
@PUT |
JAX-RS |
REST method invoked with specified HTTP method |
@Parameter |
||
@Parameters |
Wrapper for multiple @Parameter |
|
@Path |
JAX-RS |
Path to REST service or method |
@PathParam |
JAX-RS |
Name of parameter from @Path template to pass into method |
@Pattern |
BVS |
Property must match regex pattern to be valid |
@PermitAll |
EJB |
Allow any user to invoke EJB method |
@PersistenceContext |
JPA |
Inject an EntityManager |
@PersistenceUnit |
JPA |
Inject an EntityManagerFactory |
@PostActivate |
EJB3 |
Method called after bean is re-activated |
@PostConstruct |
JA |
Called after object has been constructed |
@PostParam |
JAX-RS |
|
@PreDestroy |
JA |
Called before object is destroyed |
@PrePassivate |
EJB3 |
Method called before bean is passivated |
@PrePersist |
JPA |
Invoke method before persisting new entity |
@PreUpdate |
JPA |
Invoke method before updating existing entity |
@Primary |
Spring |
give higher preference to this bean when multiple beans of same type are configured |
@Produces |
JAX-RS |
Content-type to produce and to translate return object into |
@Profile |
Spring |
Use this bean only when given profile is active |
@PropertySource |
Spring |
Read a Property file and apply values to properties that match keys |
@Qualifier |
JI |
Defines custom CDI qualifier |
@Qualifier |
Spring |
Narrow down DI choices |
@Query |
DSD |
Specifies JPA query for interface method |
@Remote |
EJB |
EJB is for remote (JNDI/RMI) access |
@Remove |
EJB |
Method disconnects client from bean and removes state from memory |
@Repository |
DS-D |
DeltaSpike Data: Interface or Abstract Class → DAO |
@RequestMapping |
SpringMVC |
Web Method URL/HTTP Method/ etc. |
@RequestParam |
SpringMVC |
Map HTTP request parameter to method parameter |
@RequestScoped |
CDI/JSF |
Bean scoped to HTTP Request |
@Required |
Spring |
indicates that the setter method must be configured to be dependency-injected |
@Resource |
JA |
Request injection of resource |
@Resources |
JA |
wrapper for multiple @Resource |
@Retention |
SE |
Whether annotion is compile time/ kept in class/ or at runtime |
@RolesAllowed |
EJB |
Specify container-managed security roles allowed to invoke method |
@RunWith |
JUnit4 |
Specify alternate test runner |
@SafeVarargs |
SE |
Compile-time - assert that annotated method uses its varargs parameter correctly |
@SandBox |
||
@Scope |
JI |
Determines lifespan of object |
@Scope |
Spring |
define the lifespan of a bean |
@SecondaryTable |
JPA |
|
@SessionScoped |
CDI/JSF |
Object scoped to HTTP Session |
@Singleton |
JI |
Instantiate EJB only once |
@Size |
BVS |
required size of field value |
@Startup |
?? |
Run at startup |
@Stateful |
EJB |
Object is Stateful Session Bean (SFSB) |
@Stateless |
EJB |
Object is Stateless Session Bean (SFSB) |
@SuppressWarnings |
SE |
Compile-time warnings suppression |
@Table |
JPA |
Specify table name etc for JPA Entity |
@Tag |
JUnit5 |
repeatable annotation declares a tag for the annotated test class or test method. |
@Tags |
JUnit5 |
container for one or more @Tag declarations. |
@Target |
SE |
Compile-time - where the annotation is allowed to be placed (classes/methods/parameters/etc.) |
@TargetApi |
Android |
API level needed for method |
@Temporal |
JPA |
Property is a date and/or time field |
@Test |
JUnit4/JUnit5 |
This is a test method |
@TestFactory |
JUnit5 |
Method is a JUnit5 test factory method. |
@TransactionAttribute |
EJB3 |
Method needs transaction |
@TransactionManagement |
EJB3 |
TX managed by bean or by container |
@TransactionScoped |
JTA |
|
@Transactional |
JTA|Spring |
Whether method needs own transaction/join existing/etc. |
@Transient |
JPA |
Omit property from database |
@UiComesAfter |
MW |
|
@UiHidden |
MW |
|
@UiLabel |
MW |
|
@Valid |
SpringMVC |
Command Object parameter is to be validated against its BVS annotations |
@Value |
Spring |
Assign default value of literal or from environment or system variable |
@Version |
JPA |
Use int or date field for optimistic locking |
@View |
||
@WebMethod |
JAX-WS |
Mark method as a SOAP Service Method (or exclude it) |
@WebParam |
JAX-WS |
Specify SOAP name for parameter |
@WebResult |
JAX-WS |
Specify SOAP name for method return value |
@WebService |
JAX-WS |
Mark object as a SOAP Web Service |
@WebServlet |
Servlet |
Mark object as an HTTP servlet and specify URL path |
@XmlAccessorType |
JAXB |
Use field or method access |
@XmlRootElement |
JAXB |
Root element of generated XML document |
@XmlType |
JAXB |
Map class to XML schema type |
APIs:
-
Android - Android SDK
-
BVS - Bean Validation Spec
-
DSD - DeltaSpike Data
-
EJB - Enterprise JavaBeans, EJB3
-
Servlet - Java EE Servlet API
-
JA - javax.annotation
-
JAX-RS - Java API for RESTful services
-
JAX-WS - Java API for XML Web Services
-
JAXB - Java API for XML binding
-
JI - javax.inject
-
JPA - Java Persistence API
-
JSF - JavaServer Faces
-
JUnit - JUnit unit testing framework
-
MW - MetaWidget dynamic-UI framework
-
SE - Java SE (standard edition)
-
Spring - Spring Framework
How we generated the list of annotations
For the start, we just grabbed all the annotations used in some code bases we have! Some funky Shell commands which will only be intelligible to the Unix cognoscenti; if that’s not you please ignore this section!
#!/bin/sh
egrep -hr '^[ \t]*@' */src/main/java |
sed -e 's/^[ \t][ \t]*//' -e 's/".*")/)/' |
tr ' ' '\n' | grep '^@' |
sort -u
That gave an initial list, which was manually edited into AsciiDoc format and formatted using AsciiDoctor-pdf. Then more annotations were generated by running the above on a larger set of source code, and also making reference to selected online documents for the various APIs.
Incomplete
This list is guaranteed to be incomplete, or your money back :-) Seriously, please use the contact form to advise of errors, omissions, additions. Thank you in advance for contributing, but please note that the editor reserves the right to include, modify or even exclude any submissions.