问题描述
启动我的应用程序时出现问题,
加载所有内容时,我有一个数据种子类
并将所有必需的数据(例如用户角色和第一个管理员用户)加载到数据库中
当我的数据库加载了数据时,一切正常,但是当使用空数据库启动应用程序时,它开始
我认为问题是-AuditorAwareImpl无法找到任何要添加created的用户。通过可选的方法,user_role记录和第一个用户记录。
这是代码:
DataSeed类:
@Component
public class DataSeed implements InitializingBean {
...
@Override
public void afterPropertiesSet() throws Exception {
for (UserRole.Role role : UserRole.Role.values()) {
createRole(role);
}
createDefaultUser();
}
private void createRole(UserRole.Role role) {
String roleCheck = role.roleName();
if (!userRoleRepository.roleExists(roleCheck)) {
userRoleRepository.save(UserRole.apply(role));
}
}
private void createDefaultUser() {
String admin = "admin";
if (!userRepository.existsByLogin(admin)) {
User defaultUser = new User();
... // default user data
userRepository.save(defaultUser);
}
}
用户实体(用户角色基于相同的基础,不会浪费时间阅读重复的代码):
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "USER_ENTITY")
public class User extends BaseEntity implements AuditInterface{
... // some static stuff
private String login;
private String password;
@ManyToMany
private List<UserRole> roles;
public User() {
}
基础实体具有附加的版本字段,并且扩展了AbstractAuditable
我还有用于审核的配置:
@Configuration
@EnableJpaRepositories(basePackages = {"com.teamcompetencymatrix.www"})
@EnableTransactionManagement
@EnableJpaAuditing
public class AuditConfig {
private final UserService userService;
public AuditConfig(UserService userService) {
this.userService = userService;
}
@Bean
public AuditorAware<User> auditorProvider() {
return new AuditorAwareImpl(userService);
}
}
和AuditorAware实施
public class AuditorAwareImpl implements AuditorAware<User> {
private UserService userService;
public AuditorAwareImpl(UserService userService) {
this.userService = userService;
}
@NonNull
@Override
public Optional<User> getCurrentAuditor() {
return userService.getAuditor();
}
}
这是错误:
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request,or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message,your code is probably running outside of dispatcherServlet: In this case,use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:313) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:66) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getobject(WebApplicationContextUtils.java:329) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getobject(WebApplicationContextUtils.java:324) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:294) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.sun.proxy.$Proxy106.getUserPrincipal(UnkNown Source) ~[na:na]
at com.teamcompetencymatrix.www.service.UserService.getAuditor(UserService.java:95) ~[classes/:na]
at com.teamcompetencymatrix.www.config.AuditorAwareImpl.getCurrentAuditor(AuditorAwareImpl.java:23) ~[classes/:na]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)