You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring项目编译报错:AppUser非托管类型致Bean创建失败

问题:Spring项目Bean创建失败,提示AppUser不是托管类型

错误详情

创建名为appUserService的Bean失败,构造方法参数0的依赖无法满足;深层原因是创建appUserRepository的Bean失败,提示com.example.chessge.appuser.AppUser不是托管类型。

已尝试操作

在ChessGeApplication类上添加了@EntityScan(basePackages = "com.example.chessge.appuser")和@EnableJpaRepositories(basePackages = "com.example.chessge.appuser")注解,但问题仍未解决。

排查方向

  • 检查AppUser类的@Entity注解包路径:Spring Boot 2.x需用javax.persistence.Entity,3.x版本需替换为jakarta.persistence.Entity,确保引入正确
  • 确认JPA依赖完整性:检查pom.xml(Maven)或build.gradle(Gradle)中是否引入spring-boot-starter-data-jpa依赖
  • 核对启动类包路径:确保ChessGeApplication在根包com.example.chessge下,若不在需调整@EntityScan和@EnableJpaRepositories的包扫描范围,覆盖到实体类和仓库所在路径
  • 清理编译缓存:执行mvn clean install(Maven)或./gradlew clean build(Gradle)重新编译项目
  • 检查关联枚举类:确认AppUserRole枚举存在且其包路径在@EntityScan的扫描范围内

相关代码

AppUser实体类

package com.example.chessge.appuser;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*;
import java.util.Collection;
import java.util.Collections;

@Entity
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class AppUser implements UserDetails {

    @Id
    @SequenceGenerator(
            name = "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "student_sequence"
    )
    private Long id;
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    @Enumerated(EnumType.STRING)
    private AppUserRole appUserRole;
    private Boolean locked = false;
    private Boolean enabled = false;

    public AppUser(String firstName,
                   String lastName,
                   String email,
                   String password,
                   AppUserRole appUserRole) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.appUserRole = appUserRole;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        SimpleGrantedAuthority authority =
                new SimpleGrantedAuthority(appUserRole.name());
        return Collections.singletonList(authority);
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}

AppUserRepository接口

package com.example.chessge.appuser;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
@Transactional(readOnly = true)
public interface AppUserRepository extends JpaRepository<AppUser, Long> {
    Optional<AppUser> findByEmail(String email);
}

AppUserService类

package com.example.chessge.appuser;

import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class AppUserService implements UserDetailsService {

    private final static String USER_NOT_FOUND_MESSAGE = "User %s not found";
    private final AppUserRepository appUserRepository;

    @Override
    public UserDetails loadUserByUsername(String email)
            throws UsernameNotFoundException {
        return appUserRepository.findByEmail(email).orElseThrow(() ->
                new UsernameNotFoundException(String.format(USER_NOT_FOUND_MESSAGE, email)));
    }
}

内容的提问来源于stack exchange,提问作者OłJeż

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 08:42:33