前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊Spring Data Auditable接口的变化

聊聊Spring Data Auditable接口的变化

作者头像
code4it
发布2018-09-17 16:50:05
7520
发布2018-09-17 16:50:05
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下Spring Data Auditable接口的变化

1.12.8.RELEASE版本

spring-data-commons-1.12.8.RELEASE-sources.jar!/org/springframework/data/domain/Auditable.java

代码语言:javascript
复制
import java.io.Serializable;

import org.joda.time.DateTime;

/**
 * Interface for auditable entities. Allows storing and retrieving creation and modification information. The changing
 * instance (typically some user) is to be defined by a generics definition.
 * 
 * @param <U> the auditing type. Typically some kind of user.
 * @param <ID> the type of the audited type's identifier
 * @author Oliver Gierke
 */
public interface Auditable<U, ID extends Serializable> extends Persistable<ID> {

    /**
     * Returns the user who created this entity.
     * 
     * @return the createdBy
     */
    U getCreatedBy();

    /**
     * Sets the user who created this entity.
     * 
     * @param createdBy the creating entity to set
     */
    void setCreatedBy(final U createdBy);

    /**
     * Returns the creation date of the entity.
     * 
     * @return the createdDate
     */
    DateTime getCreatedDate();

    /**
     * Sets the creation date of the entity.
     * 
     * @param creationDate the creation date to set
     */
    void setCreatedDate(final DateTime creationDate);

    /**
     * Returns the user who modified the entity lastly.
     * 
     * @return the lastModifiedBy
     */
    U getLastModifiedBy();

    /**
     * Sets the user who modified the entity lastly.
     * 
     * @param lastModifiedBy the last modifying entity to set
     */
    void setLastModifiedBy(final U lastModifiedBy);

    /**
     * Returns the date of the last modification.
     * 
     * @return the lastModifiedDate
     */
    DateTime getLastModifiedDate();

    /**
     * Sets the date of the last modification.
     * 
     * @param lastModifiedDate the date of the last modification to set
     */
    void setLastModifiedDate(final DateTime lastModifiedDate);
}

可以看到这个版本使用的joda-time的DateTime

2.0.7.RELEASE版本

spring-data-commons-2.0.7.RELEASE-sources.jar!/org/springframework/data/domain/Auditable.java

代码语言:javascript
复制
/**
 * Interface for auditable entities. Allows storing and retrieving creation and modification information. The changing
 * instance (typically some user) is to be defined by a generics definition.
 * 
 * @param <U> the auditing type. Typically some kind of user.
 * @param <ID> the type of the audited type's identifier
 * @author Oliver Gierke
 */
public interface Auditable<U, ID, T extends TemporalAccessor> extends Persistable<ID> {

    /**
     * Returns the user who created this entity.
     * 
     * @return the createdBy
     */
    Optional<U> getCreatedBy();

    /**
     * Sets the user who created this entity.
     * 
     * @param createdBy the creating entity to set
     */
    void setCreatedBy(U createdBy);

    /**
     * Returns the creation date of the entity.
     * 
     * @return the createdDate
     */
    Optional<T> getCreatedDate();

    /**
     * Sets the creation date of the entity.
     * 
     * @param creationDate the creation date to set
     */
    void setCreatedDate(T creationDate);

    /**
     * Returns the user who modified the entity lastly.
     * 
     * @return the lastModifiedBy
     */
    Optional<U> getLastModifiedBy();

    /**
     * Sets the user who modified the entity lastly.
     * 
     * @param lastModifiedBy the last modifying entity to set
     */
    void setLastModifiedBy(U lastModifiedBy);

    /**
     * Returns the date of the last modification.
     * 
     * @return the lastModifiedDate
     */
    Optional<T> getLastModifiedDate();

    /**
     * Sets the date of the last modification.
     * 
     * @param lastModifiedDate the date of the last modification to set
     */
    void setLastModifiedDate(T lastModifiedDate);
}

可以看到新版版本,去掉了强制依赖joda-time,改为在接口定义新增泛型来表达,该泛型要求实现TemporalAccessor接口 另外,返回值的类型都改为了Optional

小结

新版的Auditable主要有两个比较大的变动:

  • 时间类型改为泛型定义,要求具体类型实现TemporalAccessor接口
  • 返回值统一返回Optional类型

doc

  • 3.9. Auditing
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.12.8.RELEASE版本
  • 2.0.7.RELEASE版本
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档