聊聊Spring Data Auditable接口的变化

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

1.12.8.RELEASE版本

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

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

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

猜你喜欢

转载自my.oschina.net/go4it/blog/1820807