有沒有辦法從Spring AOP建議持久化JPA實體?每當我嘗試做我得到錯誤java.lang.NoSuchMethodError。我想這可能是因爲我的persistence.xml配置在運行時部署了JPA基礎架構。使用Spring AOP + JPA(EclipseLink)
Spring配置:
<aop:aspect ref="attachmentCreatorAdvice">
<aop:pointcut expression="execution(* com.mycomp.core.eventhandlers.*.handle*CreatedEvent(..))" id="attachmentCreatorPointcut"/>
<aop:after-returning method="create"
pointcut-ref="attachmentCreatorPointcut" />
</aop:aspect>
AttachmentCreatorAdvice代碼:
private AttachmentDao attachmentDao;
public AttachmentDao getAttachmentDao() {
return attachmentDao;
}
public void setAttachmentDao(AttachmentDao attachmentDao) {
this.attachmentDao = attachmentDao;
}
public void create(JoinPoint jp) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Event event = (Event) jp.getArgs()[0];
Field f = event.getClass().getDeclaredField("attachments");
if ( f != null) {
f.setAccessible(true);
List<Attachment> attachments = (List<Attachment>) f.get(event);
Field id = event.getClass().getDeclaredField("id");
if (id != null) {
id.setAccessible(true);
AggregateIdentifier uuid = (AggregateIdentifier) id.get(event);
for(Attachment attachment : attachments) {
AttachmentDto newAttachment = new AttachmentDto();
newAttachment.setId(UUID.randomUUID().toString());
newAttachment.setElementId(uuid.asString());
newAttachment.setDescription(attachment.getDescription());
newAttachment.setFilename(attachment.getFilename());
newAttachment.setFilesize(attachment.getFilesize());
newAttachment.setFiletype(attachment.getFiletype());
newAttachment.setLink(attachment.getLink());
newAttachment.setAttachmentBlob(DecodeUtils.fromBase64(attachment.getAttachmentBlob()));
attachmentDao.save(newAttachment);
}
}
}
}
attachmentDao從Spring容器注入。 AttachmentDto從AbstractDto擴展而來。
@MappedSuperclass
public abstract class AbstractDto<T> implements Serializable {
@Override
public boolean equals(Object obj) {
boolean equals = false;
if (obj instanceof AbstractDto) {
AbstractDto o = (AbstractDto) obj;
if (o.getId().equals(this.id)) {
equals = true;
}
} else {
equals = false;
}
return equals;
}
@Id
private T id;
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
}
當attachmentDao.save方法被調用我得到以下錯誤:
[EL Severe]: ejb: 2012-12-17 16:12:17.579--ServerSession(30266940)--Thread(Thread[main,5,main])--java.lang.NoSuchMethodError com.mycomp.core.data.dto.AbstractDto <init>
希望有所幫助。
乾杯
請發佈代碼您嘗試了什麼地方,正是ü獲取錯誤 – Rajesh