如何使用 java config 配置审计 [英] How to configure auditing with java config
问题描述
我正在尝试使用 Spring Data JPA 实现基本审计.从 this 问题我了解到还没有可以使用注释启用审计.所以我在 src/main/resources
中有以下 applicationContext.xml
文件:
I'm trying to implement basic auditing using Spring Data JPA. From this question I learned that it is not yet possible to enable auditing using an annotation. So I have the following applicationContext.xml
file in src/main/resources
:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:auditing />
</beans>
我已将 @ImportResources("classpath:/applicationContext.xml")
添加到我的 Java 配置文件中.
I have added the @ImportResources("classpath:/applicationContext.xml")
to my Java Config file.
在我的 AbstractEntity
(这是一个 @MappedSuperClass
)上,我有以下内容:
On my AbstractEntity
(which is a @MappedSuperClass
) I have the following:
@MappedSuperclass
public abstract class AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Version
private Integer version;
@CreatedDate
private Date createdDate;
@LastModifiedDate
private Date lastModifiedDate;
// GETTERS AND SETTERS
}
java.util.Date
的导入位置.我也试过 JodaTime
但没有改变.
Where java.util.Date
has been imported. I have also tried with JodaTime
but no change.
据我所知,此配置应该足以启用日期审计.我不需要 @CreatedBy
或 @LastModifiedBy
审计,所以我认为我不需要 AuditAware
bean...即使如此,我尝试添加它但也没有运气.
As far as I can tell this configuration should be sufficient to enable the auditing for dates. I have no need for the @CreatedBy
or @LastModifiedBy
audits, so I don't think I need an AuditAware
bean...even so, I have tried adding it but also without luck.
如何让基本审计发挥作用?
How do I get basic auditing to work?
推荐答案
首先,Spring Data JPA 1.5 M1 附带@EnableJpaAuditing,因此如果您能够升级到此,则不需要额外的 XML 文件版本.
First of all, Spring Data JPA 1.5 M1 ships with @EnableJpaAuditing so that you don't need the extra XML file if you're able to upgrade to this version.
您似乎缺少 JPA 配置(通常是 orm.xml
配置文件)中 AuditingEntityListener
的声明.请参阅参考文档 了解详情.
It seems like you're missing the declaration of the AuditingEntityListener
in the JPA config (usually the orm.xml
config file). See the reference documentation for details.
这篇关于如何使用 java config 配置审计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!