OnBeforeSave被调用但没有任何反应

时间:2018-03-28 13:50:25

标签: java spring mongodb spring-data event-listener

我正在尝试检查我试图保留的对象是否已经拥有“所有者”,如果不是我设置了登录用户。此EventListener适用于Contact的任何其他字段,它在我调试时设置用户。 MongoRepository保存方法的返回包含正确的set用户,但是当我查看数据库时,用户未被保存。

OwnedByUserMongoEventListener类

public void onBeforeSave(BeforeSaveEvent<Contact> event) {
    Contact source = event.getSource();
    ReflectionUtils.doWithFields(source.getClass(), new OwnedByUserCallback(source));
}

OwnedByUserCallback类

@Override
public void doWith(java.lang.reflect.Field field) throws IllegalArgumentException, IllegalAccessException {
    ReflectionUtils.makeAccessible(field);
    final Object fieldValue = field.get(getSource());
    if (field.isAnnotationPresent(OwnedByUser.class)) {
        if ( fieldValue == null ) {
            JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            User user = mongoOperations.findOne(Query.query(Criteria.where("username").is(principal.getUsername())), User.class);
            field.set(getSource(), user);
        }
    }

}

/

Contact extends Auditable {

    @Id
    private String id;

    @Indexed(unique=true,sparse=true)
    @NotNull
    private Long seqId;

    private BasicDBObject attributes = new BasicDBObject(new HashMap<>());

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getAttributes() {
        return attributes.toMap();
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = new BasicDBObject(attributes);
    }

    public String toString() {
        return "ID: "+getId() +" Attributes: " + getAttributes();
    }

    public Long getSeqId() {
        return seqId;
    }

    public void setSeqId(Long seqId) {
        this.seqId = seqId;
    }

}



public abstract class Auditable {

    @CreatedDate
    private Date createdDate;

    @DBRef
    @CreatedBy
    private User createdBy;

    @DBRef
    @OwnedByUser
    private User owner;

    @LastModifiedDate
    private Date updatedDate;

    @DBRef
    @LastModifiedBy
    private User updatedBy;

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    public User getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(User updatedBy) {
        this.updatedBy = updatedBy;
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

}

1 个答案:

答案 0 :(得分:0)

我尝试使用OnBeforeSave,除非更改event.getDBObject(),否则在调用MongoConverter后无法更改属性。如果你想使用你的类对象进行更改,那么你需要使用OnBeforeConvert ...由于文档很差,我花了很多时间跟踪这个......