[英]Parameter 0 of constructor in Class B required a bean of type Class A that could not be found
Am making an API authentication feature with JWT in Spring boot rest , but am facing the following error : 在Spring Boot Rest上使用JWT进行API身份验证功能,但遇到以下错误:
Description :
说明 :
Parameter 0 of constructor in com.icfer.huxy.icfer.controller.UserController required a bean of type 'com.icfer.huxy.icfer.service.ApplicationUserRepository' that could not be found.
com.icfer.huxy.icfer.controller.UserController中的构造函数的参数0需要找不到“ com.icfer.huxy.icfer.service.ApplicationUserRepository”类型的Bean。
Action :
动作 :
Consider defining a bean of type 'com.icfer.huxy.icfer.service.ApplicationUserRepository' in your configuration.
考虑在配置中定义类型为“ com.icfer.huxy.icfer.service.ApplicationUserRepository”的bean。
Below is what I have implemented regarding ApplicationUserRepository
, and UserController
classes. 以下是我针对
ApplicationUserRepository
和UserController
类实现的内容。
ApplicationUserRepository.java ApplicationUserRepository.java
import com.icfer.huxy.icfer.model.ApplicationUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
public interface ApplicationUserRepository extends JpaRepository<ApplicationUser, Long> {
ApplicationUser findByUsername(String username);
}
ApplicationUser.java ApplicationUser.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class ApplicationUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String username;
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserController.java UserController.java
import com.icfer.huxy.icfer.model.ApplicationUser;
import com.icfer.huxy.icfer.service.ApplicationUserRepository;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class UserController {
private ApplicationUserRepository applicationUserRepository;
private BCryptPasswordEncoder bCryptPasswordEncoder;
public UserController(ApplicationUserRepository applicationUserRepository,
BCryptPasswordEncoder bCryptPasswordEncoder) {
this.applicationUserRepository = applicationUserRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
@PostMapping("/sign-up")
public void signUp(@RequestBody ApplicationUser user) {
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
applicationUserRepository.save(user);
}
}
My gradle file de is as below : 我的gradle文件如下:
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-web')
compile("com.auth0:java-jwt:3.4.0")
runtimeOnly('mysql:mysql-connector-java')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.security:spring-security-test')
}
Below is the link to my project: Github link 以下是我的项目的链接: Github链接
One more reason sharing an actual sample is helpful as none of the code you've shared provides the information that we need to help you. 共享实际样本的另一个原因是有帮助的,因为您共享的代码都不提供我们需要帮助的信息。
On your @SpringBootApplication
you have the following: 在
@SpringBootApplication
您具有以下内容:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class})
public class IcferApplication { ... }
Because you are excluding DataSourceAutoConfiguration
, no DataSource
is created. 因为您要排除
DataSourceAutoConfiguration
,所以不会创建任何DataSource
。 There is no DataSource
so JPA does not kick in. JPA is not started so your JPA repository is not created. 没有
DataSource
因此JPA无法启动。JPA未启动,因此未创建JPA存储库。
You could have ran your app in debug mode (ie by adding a -Ddebug
system property): 您可能已经在调试模式下运行了您的应用程序(即,通过添加
-Ddebug
系统属性):
JpaRepositoriesAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans of type javax.sql.DataSource (OnBeanCondition)
Matched:
- @ConditionalOnClass found required class 'org.springframework.data.jpa.repository.JpaRepository' (OnClassCondition)
- @ConditionalOnProperty (spring.data.jpa.repositories.enabled=true) matched (OnPropertyCondition)
Also, please don't use EnableAutoConfiguration
together with SpringBootApplication
, there is an exclude
attribute on the latter as well. 另外,请不要将
EnableAutoConfiguration
与SpringBootApplication
一起使用,后者还具有exclude
属性。
Once I removed the exclude on DataSourceAutoConfiguration
that error went away. 一旦删除了
DataSourceAutoConfiguration
上的排除项,该错误就会消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.