【问题标题】:Using Spring AOP + JPA ( EclipseLink )使用 Spring AOP + JPA ( EclipseLink )
【发布时间】:2012-12-19 10:55:08
【问题描述】:

有没有办法从 Spring AOP 建议中持久化 JPA 实体?每当我尝试这样做时,都会收到错误 java.lang.NoSuchMethodError。我猜这可能是因为我的 persistence.xml 配置在运行时部署了 JPA 基础架构。

弹簧配置:

<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>

希望对您有所帮助。

干杯

【问题讨论】:

  • 请发布您尝试过的代码..您到底在哪里出错

标签: java spring jpa aop


【解决方案1】:

完整的异常堆栈跟踪会有所帮助,但错误似乎与 JPA 编织有关。

这意味着您的 Spring 配置或使用不正确,因为类没有正确编织。可能是您的超类未包含在您的 persistence.xml 类列表中,或者它在 Spring 调用 JPA 编织器之前被加载。

您可以尝试在您的 persistence.xml 中禁用编织(或仅禁用“eclipselink.weaving.internal”="false"),或使用静态编织,或修复您的 Spring 使用。

【讨论】:

  • 代码在其他未使用方面的场景中正常工作。我还怀疑,Spring poincut/advice bean 初始化是在编织 JPA 实体之前完成的,所以它可能导致这个错误。我将尝试将我的超类放在 persistence.xml 配置文件中。谢谢。
猜你喜欢
  • 2016-08-22
  • 2021-03-13
  • 2014-12-22
  • 1970-01-01
  • 2015-04-13
  • 2021-11-12
  • 1970-01-01
  • 2011-07-15
  • 2022-01-13
相关资源
最近更新 更多