博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Data Auditing for MongoDB
阅读量:6313 次
发布时间:2019-06-22

本文共 2489 字,大约阅读时间需要 8 分钟。

  hot3.png

#Data Auditing for MongoDB

Like the JPA auditing provided in Spring Data JPA project, in the latest Spring Data MongoDB, it provides the same feature for MongoDB. You can mark the same annotations(@CreatedDate, @CreatedBy, @LastModifiedDate, @LastModifidedBy) on your Mongo @Document class and make these fields be filled automatically at runtime.

Enable Auditing

Add @EnableMongoAuditing on your @Configuration class if you are using Java config.

<pre> @EnableMongoAuditing(modifyOnCreate=false) public class MongoConfig extends AbstractMongoConfiguration { //... } </pre>

If you prefer XML configuration, add &ltmongo:auditing... in your configuration file.

<pre> &lt;mongo:auditing auditor-aware-ref="auditor" modify-on-creation="false"/> </pre>

Implement @AuditorWare interface

It is the same interface we have discussed in the Data JPA auditing example.

<pre> @Named(value="auditor") public class Auditor implements AuditorAware<String> { @Override public String getCurrentAuditor() { return "hantsy"; } } </pre>

Example

Change the Conference class to the following. Add @CreatedDate, @CreatedBy, @LastModifiedDate, @LastModifidedBy to verify the auditing features.

<pre> @Document public class Conference { @Id private String id; @NotNull private String name; @NotNull private String description; @CreatedBy private String createdBy; @CreatedDate private Date createdDate; @LastModifiedBy private String lastModifiedBy; @LastModifiedDate private Date lastmodifiedDate; } </pre>

Now write some codes to test it.

<pre> @Test public void retrieveConference() { Conference conference = newConference(); conference = conferenceRepository.save(conference); assertTrue(null != conference.getId()); conference = conferenceRepository.findByName("JUD2013"); assertTrue(null != conference); assertTrue("hantsy".equals(conference.getCreatedBy())); assertTrue(conference.getCreatedDate()!=null); log.debug("conference.getLastModifiedBy()@"+conference.getLastModifiedBy()); log.debug("conference.getLastmodifiedDate()@"+conference.getLastmodifiedDate()); assertTrue(conference.getLastModifiedBy()==null); assertTrue(conference.getLastmodifiedDate()==null); conference.setName("test"); conference=conferenceRepository.save(conference); assertTrue("test".equals(conference.getName())); assertTrue(conference.getLastModifiedBy()!=null); assertTrue(conference.getLastmodifiedDate()!=null); } </pre>

Check out the sample codes from my github.com account, .

转载于:https://my.oschina.net/hantsy/blog/292997

你可能感兴趣的文章
cento7下创建mysql5.7的双向主从
查看>>
单例-线程池demo
查看>>
ServletRequest listener
查看>>
Django实现网站登陆的功能
查看>>
Cacti监控windows无法取得监控项
查看>>
从零开始设计一个管理系统
查看>>
直连不通网段间通信问题
查看>>
如何应对BYOD时代的安全风险
查看>>
倒计时
查看>>
在windows下部署包含C3P0的war包没问题,部署到linux下面的tomcat下C3P0报错
查看>>
nginx负载均衡配置
查看>>
Centos 7 添加epel源
查看>>
mac使用pip3报错
查看>>
bug2 The method of type must override a superclass method解决方式(去掉@override可以)
查看>>
python 中datetime 和 string 转换
查看>>
C# 最简单的异步委托
查看>>
使用Java开发微信公众平台(二)——消息的接收与响应
查看>>
常微分方程_阿诺尔德 1.1节,问题6 擴張相空間沿時間軸的平移變換將積分曲線變爲積分曲線...
查看>>
幂函数的连续性
查看>>
aria-hidden读屏
查看>>