Annotations not found on the object while maintaining runtime

Ok, I'm a little confused. I am trying to select the "DAO" class using the model annotation:

@Entity @Table(name="dispatcher") // use the Kamailio Base DAO for code that supports this annotation @DAOSelector(dao = DAOBaseKamailio.class) public class DispatcherSet extends Model { [...] } 

The following is a description of the annotation:

 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface DAOSelector { Class<?> dao(); } 

I use the following code to return the correct DAO class:

 public static DAOInterface getCorrectDAO(final Object object) throws Exception { final DAOSelector annotation = object.getClass().getAnnotation(DAOSelector.class); if(annotation != null) { System.out.println("Annotation present: " + annotation.dao().getName() + " for class " + object.getClass().getName()); final Object dao = annotation.dao().newInstance(); if(!(dao instanceof DAOInterface)) { throw new Exception("Invalid Base DAO in annotation for entity " + object.getClass().getName()); } return (DAOInterface) dao; } else { System.out.println("Annotation not present for class " + object.getClass().getName()); return new DAOBase(); } } 

However, when I root the DispatcherSet annotation, it is always null:

10: 33: 38,498 [INFO] [STDOUT] Class annotations are not present. DispatcherSet

Did I miss something?

edit:

OK, I found something interesting, I run this code inside the JBoss container and when I print out all the annotations:

 {{{ $Proxy76 $Proxy708 $Proxy77 }}} 

One of them should be a proxied instance of the DAOSelector annotation, which I assume, therefore, probably why getAnnotation(DAOSelector.class) will not work by checking it.

edit2:

No, they are not instances of DAOSelector

+6
source share
1 answer

I fixed the problem. it was a class problem. I have an ear with a can and a war. The model was in the bank, and the annotation was present in both.

+3
source

Source: https://habr.com/ru/post/906377/


All Articles