当前位置: 首页 > 知识库问答 >
问题:

接收“通过字段“userService”表示的未满足的依赖项;嵌套异常”

欧阳斌
2023-03-14

不知道这里发生了什么。错误是

在上下文初始化过程中遇到的异常-取消刷新尝试:org.springframework.beans.factory.未满足依赖异常:创建名称为“身份验证控制器”的bean时出错:通过字段“用户服务”表示的未满足依赖;嵌套异常org.springframework.beans.factory.未满足依赖异常:创建名称为“用户服务”的bean时出错-通过构造函数参数1表示的未满足依赖”

我错过什么了吗?

我的控制器-

import com.***.model.User;
import com.***.service.AuthTokenService;
import com.***.service.Authentication;
import com.***.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;


@Controller
@RestController
@RequestMapping("/api/v1")
public class AuthenticationController {

@Autowired
UserService userService;
AuthTokenService authTokenService;

@GetMapping(path = "/users")
public ResponseEntity<?> listUser() {
    List<User> resource = userService.getUser();
    return ResponseEntity.ok(resource);
}

@PostMapping(path = "/register")
public ResponseEntity<?> register(@RequestBody User newUser) {
    User user = userService.findUserByEmail(newUser.getEmail());
    User unconfirmedUser = userService.registerUser(newUser);
    return ResponseEntity.ok(unconfirmedUser);
}

我的用户服务-

import com.***.model.User;
import com.***.repository.UserRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

    @Service
    @Component
    @AllArgsConstructor
    public class UserService {

    @Autowired
    UserRepository userRepository;
    AuthTokenService authTokenService;
    EmailSenderService emailSenderService;

    void sendAuthenticationEmail(String userMail, String token) {
        final SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(userMail);
        mailMessage.setSubject("Mail Confirmation Link!");
        mailMessage.setFrom("<Mail Service>");
        mailMessage.setText( "Thank you for joining ***! Please click below to activate your account." + "http://8080/api/v1/confirm?token=" + token);
        emailSenderService.sendEmail(mailMessage);
    }

    public User registerUser(User user) {
        final Authentication authenticationToken = new Authentication(user);
        authTokenService.saveAuthenticationToken(authenticationToken);
        sendAuthenticationEmail(user.getEmail(), authenticationToken.getUserToken());
        return userRepository.save(user);
    }

    public void confirmUser(Authentication authenticationToken) {
        final User user = authenticationToken.getUser();
        user.setEnabled(true);
        userRepository.save(user);
        authTokenService.deleteAuthenticationToken((long) authenticationToken.getId());
    }
}

认证-

import com.***.model.User;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.UUID;


@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Authentication {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String userToken;
    private LocalDate dateTimeCreated;

    @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)
    @JoinColumn(nullable = false, name = "id")
    private User user;

    Authentication(User user) {
        this.user = user;
        this.dateTimeCreated = LocalDate.now();
        this.userToken = UUID.randomUUID().toString();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserToken() {
        return userToken;
    }

    public void setUserToken(String userToken) {
        this.userToken = userToken;
    }

    public LocalDate getDateTimeCreated() {
        return dateTimeCreated;
    }

    public void setDateTimeCreated(LocalDate dateTimeCreated) {
        this.dateTimeCreated = dateTimeCreated;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

认证令牌回购-

package com.***.repository;

import com.***.service.Authentication;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface AuthenticationTokenRepository extends CrudRepository<Authentication, Long> {

    Optional<Authentication> findAuthenticationToken(String token);

}

这里是文件结构的图片

错误日志

共有1个答案

苏德容
2023-03-14

基于您的错误,您似乎不知道如何创建基于JpqRepository方法签名的查询。你可以在这里阅读:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-方法

我猜AuthenticationTokenRepository中名为findAuthenticationToken(String someField)的方法应该命名为findBySomeField(String someField)。还要确保你有某个领域的getter

如果你能提供你的身份验证令牌库和身份验证类,会更容易

编辑:

将您的方法在身份验证令牌库中更改为findByUserToken(字符串用户令牌)

 类似资料: