Ok, tôi hơi bối rối ở đây. Tôi đang cố gắng để chọn một lớp "DAO" bằng cách sử dụng một chú thích trên mô hình:Không tìm thấy chú thích trên đối tượng có lưu giữ Thời gian chạy
@Entity
@Table(name="dispatcher")
// use the Kamailio Base DAO for code that supports this annotation
@DAOSelector(dao = DAOBaseKamailio.class)
public class DispatcherSet extends Model {
[...]
}
Đây là defenition chú thích:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DAOSelector {
Class<?> dao();
}
tôi sử dụng đoạn mã sau để trả lại "DAO thích "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();
}
}
Tuy nhiên, khi tôi nuôi một chú thích DispatcherSet
đối tượng luôn luôn là null:
10: 33: 38.498 [INFO] [STDOUT] Chú không có mặt cho lớp model.DispatcherSet
Tôi có thiếu cái gì ở đây?
chỉnh sửa:
OK, tìm thấy một cái gì đó thú vị, tôi đang chạy mã này bên trong một container JBoss và khi tôi in ra tất cả các chú thích:
{{{
$Proxy76
$Proxy708
$Proxy77
}}}
Một trong số đó phải là một ví dụ proxy của chú thích DAOSelector
tôi đoán, vì vậy đó có thể là lý do tại sao getAnnotation(DAOSelector.class)
không hoạt động, hãy kiểm tra.
edit2:
Không, họ không phải là một thể hiện của DAOSelector