Using jersey - I know I can annotate a method with
#Path("/{a:path1|path2}")
but I was wandering if it was also possible to just use two annotations on a single method
#Path("path1")
#Path("path2")
To get almost the same effect (I know - this way I can not get a #PathParam ).
Only in Java 8 it is allowed to have more than one same annotation. And even more than that annotation should be marked specially (#Repeatable, see more info here). Annotation #Path does not have such meta-annotation.
So, conclusion it is not possible.
Related
I have written a MapStruct mapper which has two methods with the #AfterMapping annotation. Depending on the context, I want only one of these methods to be executed before the end of the mapping.
With regard to this, the MapStruct documentation states:
All after-mapping methods that can be applied to a mapping method will
be used. #Qualifier / #Named can be used to filter the methods to use.
Regarding #Named, the documentation contains a good example on how to use this annotation in order to perform two different kinds of mappings for a bean property. But I do not understand how to adapt this kind of distinction to my two after-mapping methods.
You can specify the qualifier in the #BeanMapping annotation in order to make it applicable to life cycle method. Note: I recently fixed an issue in this area on the master. Don't think that's released already. Forgot the exact issue (not able to look it up currently)
In our application, we use a graph persistence library which do not implement JPA entity listeners behaviours. In other words, a method annotated with #PrePersist will never be called.
As we discovered the need for such a behaviour, I would like to know if there is a java library that could ease things a little ... as an example by returning me a list of objects/methods that need to be called as #PrePersist elements. Do you know such libraries ?
You can try Reflections
Reflections scans your classpath, indexes the metadata, allows you to query it on runtime and may save and collect that information for many modules within your project.
Using Reflections you can query your metadata such as:
get all subtypes of some type
get all types/methods/fields annotated with some annotation, w/o annotation parameters matching
get all resources matching matching a regular expression
Hibernate Search, Hibernate, Struts2... I see more examples... In same examples I see the annotation on the field.. Other on the get/set method.. There are differences? Or is casual..
I hope that is not a stupid question!
Saluti!
Luigi
The difference depends on the annotation and how it is used. For example, in Spring you can use the #Controller annotation only on a class. This tells Spring that the class is a controller.
As far as methods are concerned, #RequestMapping is an annotation that goes on a method. For properties, you can have validation annotations like #NotNull (in Hibernate validator).
Annotations are definitely not casual; they carry meaning and can affect the way the code behaves.
From the Java documentation regarding annotations:
Annotations provide data about a
program that is not part of the
program itself. They have no direct
effect on the operation of the code
they annotate.
Annotations have a number of uses,
among them:
Information for the compiler — Annotations can be used by the
compiler to detect errors or suppress
warnings.
Compiler-time and deployment-time processing — Software tools can
process annotation information to
generate code, XML files, and so
forth.
Runtime processing — Some annotations are available to be
examined at runtime.
Annotations can be applied to a
program's declarations of classes,
fields, methods, and other program
elements.
You can specify what an annotation can annotate by specifying the the elements (using a #Target annotation) when you define your own annotation.
This really depends on the code that interprets the annotations. It can of course make a difference, but the annotations you are talking about are probably meant to annotate a "property", which is something that technically does not exist in Java.
Java has fields and methods, but these are used to simulate properties under the "Java Bean" conventions, i.e. you have a public setX() and a getX() method that often (but not always) write and read a private field x. They're tied together via a naming condition, not a language mechanism.
Because of that, most frameworks that use annotations for such properties (e.g. for persistence mapping or dependency injection) are flexible and allow you to annotate either the field or the get or set method.
I'm trying to set up a Hibernate filter with annotations. I would like to specify it in a base class and make all sub classes use it but whenever I try to enable it, Hibernate fails to find the filter. Is it possible at all to inherit filter annotations?
Since 3.5.0 it's at least possible for #MappedSuperclass.
Not sure if that helps you... see: HHH-4332
Are you using the hibernate filter directly, or are you extending the hibernate filter for your own purposes? Annotations aren't inherited by default in Java, although if you were writing the annotation yourself, and I'm assuming hibernate didn't do this on their annotations, you can specify the #Inherited meta-annotation on your annotation to make it inherit. This only works for Type level annotations, though. Also, some people write an annotation manager to simulate full inheritance. You could do something like that, either extend hibernates mechanism or write a preprocessor that adds the annotations where they should have been inherited.
What John Ellinwood wrote is precisely the case. That is why #Filter needs to be specified on all subclasses, at least in Hibernate Annotations 3.4.0-GA.
Furthermore, although that might not be apparent, you will need the same annotation on all mapped collections of that class, if you expect those collections to be susceptible to filtering as well.
Are there any commonly usable annotations available? Similar to commons-lang?
If not, have you seen any effective use of annontations (not built-in annotations) as part of any open source application development.
I remember Mifos was using it for Transaction.
Mohan
i think Hibernate Validator has really good and reusable annotations for any kind of validation. it is based on a the reference implementation for JSR 303: Bean Validation.
Only non-standard annotations I've used more than once outside my testing project have been WicketStuff Annotations which are very useful in their own context.
Another interesting annotation set which is also the basis for JSR-305 is FindBugs' annotations which also may prove useful in the future - we'll see how that goes.
Check out my Bean annotations
http://code.google.com/p/javadude/wiki/Annotations
Things like
#Bean(
cloneable=true,
defineSimpleEqualsAndHashCode=true,
properties={
#Property(name="name", bound=true),
#Property(name="age", type=int.class, bound=true),
#Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
},
observers={
#Observer(type=FeverListener.class)
}
)
public class Person extends PersonGen { }
The annotation processor generates the PersonGen superclass.
Note that I'm currently working on a major change to them and the API is changing (I'll still leave the current version available, but the 3.x.x version stream will be breaking)
I'm trying to get the new version done in the next couple of weeks.
JAXB defines annotations (javax.xml.bind.annotation) that are reused to some degree -- although they are named to indicate they only related to XML serialization, most of metadata has to do with annotating properties to serialize, so they can be used for serializing to other data formats (such as JSON) too. Jackson JSON processor supports them, along its own 'native' annotations, since there are no really standardizes non-data-format specific annotations (AFAIK).
I like and Oval http://oval.sourceforge.net/ and JAXB
there really needs to be a set of common annotationsin the core jre which are used in similar ways in multiple frameworks.
for example #Transactional #Nullable