gpt4 book ai didi

java - 是否可以在 AuditorAwareImpl 类中使用存储库?

转载 作者:行者123 更新时间:2023-12-02 09:31:10 30 4
gpt4 key购买 nike

我想在数据库中实现一个引用,该引用是谁以及何时创建/更新实体的。createdAt 和updatedAt 日期已经可以使用。

但是我可以看到,当我设置断点时,getCurrentAuditor 方法的返回被调用大约十次,直到客户端收到响应。响应是一个带有 CONFLICT HttpStatus-Code 的错误。

我有一个名为BaseEntity的类,其中定义了属性。

请参阅下面我的类(class):

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();
}

}


从 Zorglube 编辑 v1(对我不起作用)

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"));
}
}

最佳答案

试试这个:

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);
}

}

关于java - 是否可以在 AuditorAwareImpl 类中使用存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957103/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com