com.earworm.backendearworm.UserService中构造函数的参数0需要一个类型的bean.&



我和我的团队在让我们的spring引导应用程序运行时遇到了麻烦,我们试图修复一个问题,然后导致另一个错误。我一直在四处寻找,但似乎找不到答案。这是我们项目的github的链接。https://github.com/matfitchell/Earworm/tree/main/SourceCode/BackEnd/src/main/java/com/earworm

尝试运行spring boot时出现错误:

com.earworm.backendearworm中构造函数的参数0。UserService需要com.earworm.backendearworm.UserRepository类型的bean,但找不到

我已经尝试添加不同的依赖到我们的项目。UserService.java

package com.earworm.backendearworm;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import com.earworm.registration.token.ConfirmationToken;
import com.earworm.registration.token.ConfirmationTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService implements UserDetailsService {
private final UserRepository userRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final ConfirmationTokenService confirmationTokenService;
public UserService(UserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder,
ConfirmationTokenService confirmationTokenService) {
this.userRepository = userRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
this.confirmationTokenService = confirmationTokenService;
}
public List<User> getUsers() {
return userRepository.findAll();
}


public String addNewUser(User user) {
Optional<User> userByEmail = userRepository.findUserByEmail(user.getEmail());
if (userByEmail.isPresent()) {
throw new IllegalStateException("Email taken");
}
String encodedPassword = bCryptPasswordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);
userRepository.save(user);
String token = UUID.randomUUID().toString();
ConfirmationToken confirmationToken = new ConfirmationToken(token, LocalDateTime.now(),
LocalDateTime.now().plusMinutes(15), user);
confirmationTokenService.saveConfirmationToken(confirmationToken);
return token;
}
*/
/*
* Fix exists by ID by actually passing id and changing repository from string
* to long
*/
public void deleteUser(Long id) {
boolean userExists = userRepository.existsById(id);
if (!userExists) {
throw new IllegalStateException("User with id " + id + " does not exist");
}
userRepository.deleteById(id);
}
// Name and email are optional
@Transactional
public void updateUser(Long id, String username, int zipCode, String bio) {
User user = userRepository.findById(id)
.orElseThrow(() -> new IllegalStateException("User with id " + id + " does not exist"));
if (username != null && username.length() > 0 && !Objects.equals(user.getUsername(), username)) {
user.setUsername(username);
}
// -------Change email if wanted-------
// if (email != null && email.length() > 0 && !Objects.equals(user.getEmail(),
// email)) {
// Optional<User> userOptional = userRepository.findUserByEmail(email);
// if (userOptional.isPresent()) {
// throw new IllegalStateException("Email taken");
// }
// user.setEmail(email);
// }
// Fix to lang and
// long?>----------------------------------------------------------------------------------
// user.setZipCode(zipCode);
user.setBio(bio);
}
/* 
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
return userRepository.findUserByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("user with email " + email + " not found"));
}
public int enableUser(String email) {
return userRepository.enableUser(email);
}
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// TODO Auto-generated method stub
return null;
}
}

UserRepository.java

package com.earworm.backendearworm;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT s FROM User s WHERE s.email = ?1")
Optional<User> findUserByEmail(String email);
// List<User> findAllByZipcode(int zipCode);
// @Query("SELECT s FROM User s WHERE s.username = ?1")
Optional<User> existsByUsername(String username);
@Transactional
@Modifying
@Query("UPDATE user a " + "Set a.enabled = TRUE WHERE a.email = ?1")
int enableUser(String email);
}

我检查了你的repo中的代码,发现你排除了DataSourceAutoConfiguration.class。这就产生了一个问题,因为Spring不能创建数据库连接,没有连接的Repository类bean将无法在Spring上下文中使用。

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@RestController
@EnableAutoConfiguration
@ComponentScan
public class BackdendEarwormApplication {
public static void main(String[] args) {
//code
}
}

所以请删除此排除并再试一次。此外,将BackdendEarwormApplication类移动到com.earworm包中,以便在Spring上下文中可以使用子包(backendearworm,email,registration,security和spotifyAPI)中的所有类。

最新更新