gpt4 book ai didi

java - Spring Data 2.0.x AuditorAware getCurrentUser 方法抛出 AbstractMethodError

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:20:40 35 4
gpt4 key购买 nike

我正在尝试实现 AuditorAware进入我的网络应用程序。以下是我的库版本列表:

  • jsf 2.2.13
  • hibername 5.1.3.Final
  • Spring 5.0.3.RELEASE
  • spring-data 2.0.3.RELEASE
  • spring-security 5.0.1.RELEASE

这是我的持久性配置文件:

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
public class PersistenceContext
{
@Bean
public AuditorAware<AccountModel> auditorAware()
{
return new AuditorAwareImpl();
}
}

这是我的 AuditorAwareImpl 类:

public class AuditorAwareImpl implements AuditorAware<AccountModel>
{
@Override
public Optional<AccountModel> getCurrentAuditor()
{
return Optional.ofNullable((AccountModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
}
}

账户模型:

@Entity(name = "Account")
@Table(name = "account")
public class AccountModel extends BaseModel implements UserDetails
{
private static final long serialVersionUID = -4792614546550769652L;

@Column(name = "username", length = 20, nullable = false)
private String username;

@Column(name = "password", length = 100, nullable = false)
private String password;

@Column(name = "email", length = 100, nullable = false)
private String email;

@Column(name = "accPackage", length = 2, nullable = false)
private String accPackage;

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
private Set<RoleModel> role = new HashSet<RoleModel>();

@Transient
private transient Set<GrantedAuthority> authorities;
}

基础模型:

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseModel implements Serializable
{
private static final long serialVersionUID = -8027746305948051121L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Integer id;

@CreatedBy
@ManyToOne
@JoinColumn(name = "createdBy", nullable = true, updatable = false)
private AccountModel createdBy;

@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "dateCreated", nullable = false, updatable = false)
private Date dateCreated;

@LastModifiedBy
@ManyToOne
@JoinColumn(name = "modifiedBy", insertable = false)
private AccountModel modifiedBy;

@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "dateModified", insertable = false)
private Date dateModified;

@Version
@Column(name = "verCtrl", nullable = false)
private Integer version;
}

当我尝试持久化一个对象时,将抛出 AbstractMethodError:

Caused by: java.lang.AbstractMethodError: com.project.core.AuditorAwareImpl.getCurrentAuditor()Ljava/lang/Object;
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy71.getCurrentAuditor(Unknown Source)
at org.springframework.data.auditing.AuditingHandler.touchAuditor(AuditingHandler.java:166)
at org.springframework.data.auditing.AuditingHandler.touch(AuditingHandler.java:145)
at org.springframework.data.auditing.AuditingHandler.markCreated(AuditingHandler.java:125)
at org.springframework.data.jpa.domain.support.AuditingEntityListener.touchForCreate(AuditingEntityListener.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.jpa.event.internal.jpa.ListenerCallback.performCallback(ListenerCallback.java:35)
... 124 more


更新

在我将我的 Spring Data 从 2.x 更改为 1.x 后,它可以正常工作,不再有异常。我认为问题出在 getCurrentUser() 的返回数据类型上方法。在 1.x 中,返回数据类型只是您自定义的 UserDetails对象,但在 2.x 中,返回数据类型为 Optional<CustomModel> .会不会是错误的原因?

最佳答案

是的,此错误的原因是 getCurrentUser() 的返回数据类型发生了变化方法。在 2.x 版本中,这被更改为 Optional(在 1.x 版本中,返回类型只是 T)。有两种方法可以解决这个问题:

  1. 将 Spring Data 从 2.x 降级到 1.x
  2. 但是,由于我必须将 Spring Data 保留在 2.x 版本上,所以我只是将代码从

    @Override
    public String getCurrentAuditor() {
    return currentUser();
    }

    @Override
    public Optional<String> getCurrentAuditor() {
    return Optional.of(currentUser());
    }

关于java - Spring Data 2.0.x AuditorAware getCurrentUser 方法抛出 AbstractMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023607/

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