2012-12-19 67 views
-2

有沒有辦法從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> 

希望有所幫助。

乾杯

+2

請發佈代碼您嘗試了什麼地方,正是ü獲取錯誤 – Rajesh

回答

1

完整的異常堆棧跟蹤會有所幫助,但這個錯誤似乎是它可能與JPA織。

這意味着你的Spring配置或用法不正確,因爲類沒有正確編織。這可能是因爲你的超類沒有包含在你的persistence.xml類列表中,或者它在Spring調用JPA編織器之前被加載。

您可以嘗試在persistence.xml中禁用織造(或者只是禁用「eclipselink.weaving.internal」=「false」),或者使用靜態編織或修復Spring使用。

+0

代碼在其他情況下未正常使用的情況下正常工作。我還懷疑Spring的poincut/advice bean初始化是在編織JPA實體ocurrs之前完成的,因此它可能導致這個錯誤。我會嘗試把我的超類放在persistence.xml配置文件中。謝謝。 – Nedo