转载 

Spring Data Jpa之Auditing——AuditingEntity:Spring Data JPA Tutorial: Auditing(Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者)

分类:    1057人阅读    IT小君  2017-06-14 10:28

干啥的?

When we hear the word auditing, the first thing that comes to mind is an audit log that contains each version of the audited entity. Implementing an audit log is a complex task that takes a lot of time. Luckily, most of the time we don’t need to do it.

However, it is quite common that we must be able to answer to the following questions:

  • When the entity X was created and/or modified?


  • Who created and/or modified the entity X?

https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-auditing-part-two/

http://www.jianshu.com/p/14cb69646195

怎么用?

零) 这是干什么呢? 
随意翻看spring-data-jpa (以下简称sdj)文档时发现有这个有趣的小东西, 
sdj提供了几个有趣的元注释用在实体类上,作为对javax.persistence.*元注释的扩展。 

  • @CreatedDate
  • @CreatedBy
  • @LastModifiedDate
  • @LastModifiedBy

有了这几个元注释以后就会在实体创建或更新的时候把操作时间或操作人一并更新到数据库里去, 
这个很方便。 

一) 环境 
  • springframework (3.2.4.RELEASE)
  • spring-data-jpa (1.3.4.RELEASE)
<dependency>  
    <groupId>org.springframework.data</groupId>  
    <artifactId>spring-data-jpa</artifactId>  
    <version>1.3.4.RELEASE</version>  
    <exclusions>  
        <exclusion>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjrt</artifactId>  
        </exclusion>  
        <exclusion>  
            <groupId>org.slf4j</groupId>  
            <artifactId>jcl-over-slf4j</artifactId>  
        </exclusion>  
    </exclusions>  
</dependency>  
二) 为要监控的实体加上EntityListener 
可以使用元注释或orm.xml,两者二选其一 
2.1 标注 

import javax.persistence.*;  
import org.springframework.data.annotation.*;  
import org.springframework.data.jpa.domain.support.AuditingEntityListener;  
  
@Entity  
@EntityListeners({AuditingEntityListener.class})  
public class SomeEntity {  
  
    @Id  
    private Integer id;  
      
    @CreatedDate  
    private Long createdDate;  
      
    @CreatedBy  
    private User user;  
      
    @LastModifiedBy  
    private User lastModeiUser;  
      
    // 构造  
    public SomeEntity() {  
    }  
      
    // getter & setter  
}  
2.2 xml方式 

<persistence-unit-metadata>  
    <persistence-unit-defaults>  
        <entity-listeners>  
            <entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />  
        </entity-listeners>  
    </persistence-unit-defaults>  
</persistence-unit-metadata>  
2.3 实现org.springframework.data.domain.AuditorAware<T>, 这个接口负责从安全上下文中获取系统的用户信息。我们这里以apache-shiro作为安全框架! 

import domain.User;  
  
import org.apache.shiro.SecurityUtils;  
import org.apache.shiro.subject.Subject;  
import org.springframework.data.domain.AuditorAware;  
  
public class UserAuditorAware implements AuditorAware<User> {  
  
    @Override  
    public User getCurrentAuditor() {  
        Subject subject = SecurityUtils.getSubject();  
        Object object = subject.getPrincipal();  
        return (User) object;  
    }  
  
}  
2.4 最后配置一下 

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">  
  
    <jpa:repositories base-package="pkg.of.repository" />  
      
    <bean id="userAuditorAware" class="auditor.UserAuditorAware" />  
    <jpa:auditing auditor-aware-ref="userAuditorAware" />  
  
</beans> 

支付宝打赏 微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者

 工具推荐 更多»