I want use @Nullable annotation to eliminate NullPointerExceptions. I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable; but when I import it a compilation error is generated: cannot find symbol Answer You need to include a jar that this class exi…
Tag: annotations
How @Target(ElementType.ANNOTATION_TYPE) works
Java annotations are marked with a @Target annotation to declare possible joinpoints which can be decorated by that annotation. Values TYPE, FIELD, METHOD, etc. of the ElementType enum are clear and simply understandable. Question WHY to use @Target(ANNOTATION_TYPE) value? What are the annotated annotations g…
How to disable @PostConstruct in Spring during Test
Within a Spring Component I have a @PostConstruct statement. Similar to below: During Unit tests I would not like to have the @PostConstruct function called, is there a way to telling Spring not to do post processing? Or is there a better Annotation for calling a initiation method on a class durning non-testi…
Difference between @size(max = value ) and @min(value) and @max(value)
I want to do some domain validation. In my object I have one integer. Now my question is: if I write and If it’s an integer which one is proper for domain validation? Can anybody explain me what is the difference between them? Thanks. Answer @Min and @Max are used for validating numeric fields which cou…
Passing annotation properties to meta-annotations
Say I have an annotation with a property: and I want to create a compound annotation with several meta-annotations, including the one that takes a property Is there a way that I can pass properties to the compound annotation to one of the meta annotations? Eg, something like this: that is equivalent to, but m…
An alternative to @Value annotation in static function
It’s not possible to use @Value on a static variable. When I do this, 0 is printed. So what is a good alternative to this? Answer Spring inject noting in static field (by default). So you have two alternatives: (the better one) make the field non static (the ugly hack) add an none static setter which wr…
Java seek a method with specific annotation and its annotation element
Suppose I have this annotation class So is there a way to look into an object, “seek” out the method with the @MethodXY annotation, where its element x = 3, y = 2, and invoke it? Thanks Answer Here is a method, which returns methods with specific annotations: It can be easily modified to your spec…
Implement a simple factory pattern with Spring 3 annotations
I was wondering how I could implement the simple factory pattern with Spring 3 annotations. I saw in the documentation that you can create beans that call the factory class and run a factory method. I was wondering if this was possible using annotations only. I have a controller that currently calls MyService…
Hibernate : How override an attribute from mapped super class
The generic entity, super class: The pojo: I try to use thoses annotations : @AttributeOverride, @Id, … but It doesn’t work. Can you help me? I want to override the attribute “id” to specify another column name and a sequence by pojo/table. What is the best way to do that? Answer Try t…
Default properties of Java Annotation
What are the exact default values of two meta annotations (Target and Retention) in a user defined annotation? Answer According to the source code, none of them has a default value, which means you must provide it, whenever you use the annotation. The meaning of the missing annotation is defined in the javado…