我想在数据库中实现创建或更新实体的对象和对象的引用。 createdAt和updatedAt日期已经可以使用了。
但是我可以看到,当我设置一个断点时,getCurrentAuditor方法的返回将被调用大约十次,直到响应到达客户端为止。而且响应是 CONFLICT HttpStatus-Code的错误。
我有一个名为 BaseEntity 的类,其中定义了属性。
在下面查看我的课程:
class AuditorAwareImpl implements AuditorAware<Person> {
@Autowired
private PersonRepository personRepo;
@Override
public Optional<Person> getCurrentAuditor() {
return Optional.of(personRepo.findByUserPrincipalName(SecurityContextHolder.getContext().getAuthentication().getName() + "@email.com"));
}
}
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
class JpaConfig {
@Bean
public AuditorAware<Person> auditorProvider() {
return new AuditorAwareImpl();
}
}
@Data
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
protected Long id;
@CreatedDate
private Date createdAt;
@CreatedBy
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(referencedColumnName="id")
private Person createdBy;
@LastModifiedDate
private Date updatedAt;
@LastModifiedBy
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(referencedColumnName="id")
private Person updatedBy;
@PrePersist
protected void prePersist() {
if (this.createdAt == null) createdAt = new Date();
if (this.updatedAt == null) updatedAt = new Date();
}
@PreUpdate
protected void preUpdate() {
this.updatedAt = new Date();
}
@PreRemove
protected void preRemove() {
this.updatedAt = new Date();
}
}
class AuditorAwareImpl implements AuditorAware<Person> {
@Autowired
private ApplicationContext context;
@Override
public Optional<Person> getCurrentAuditor() {
PersonRepository personRepo = context.getBean(PersonRepository.class);
return Optional.of(personRepo.findByUserPrincipalName(SecurityContextHolder.getContext().getAuthentication().getName() + "@email.com"));
}
}
答案 0 :(得分:1)
尝试一下:
class AuditorAwareImpl implements AuditorAware<Person> {
@Override
public Optional<Person> getCurrentAuditor() {
PersonRepository personRepo = ApplicationContextProvider.getApplicationContext().getBean(PersonRepository.class)
return Optional.of(personRepo.findByUserPrincipalName(SecurityContextHolder.getContext().getAuthentication().getName() + "@email.com"));
}
}
@Component(value = "applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
private static class AplicationContextHolder {
private static final InnerContextResource CONTEXT_PROV = new InnerContextResource();
}
private static final class InnerContextResource {
private ApplicationContext context;
private void setContext(ApplicationContext context) {
this.context = context;
}
}
public static ApplicationContext getApplicationContext() {
return AplicationContextHolder.CONTEXT_PROV.context;
}
@Override
public void setApplicationContext(ApplicationContext ac) {
AplicationContextHolder.CONTEXT_PROV.setContext(ac);
}
}