简体   繁体   English

B类中构造函数的参数0需要一个找不到A类类型的Bean

[英]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. 以下是我针对ApplicationUserRepositoryUserController类实现的内容。

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. 另外,请不要将EnableAutoConfigurationSpringBootApplication一起使用,后者还具有exclude属性。

Once I removed the exclude on DataSourceAutoConfiguration that error went away. 一旦删除了DataSourceAutoConfiguration上的排除项,该错误就会消失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Controller 中构造函数的参数 0 需要一个找不到的 Service class 类型的 bean - Parameter 0 of constructor in Controller required a bean of type Service class that could not be found &quot;&quot; 中构造函数的参数 0 需要一个无法找到的类型为 &quot;&quot; 的 bean - Parameter 0 of constructor in "" required a bean of type "" that could not be found “ ”中构造函数的参数 0 需要找不到类型为“ ”的 bean - Parameter 0 of constructor in ' ' required a bean of type ' ' that could not be found 构造函数中的参数 3 需要一个无法找到的“包”类型的 bean - Parameter 3 of constructor in required a bean of type 'package' that could not be found 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found “服务”中构造函数的参数 0 需要找不到类型为“存储库”的 bean - Parameter 0 of constructor in 'Service' required a bean of type 'Repository' that could not be found *Service 中构造函数的参数 0 需要找不到类型为“*Repository”的 bean - Parameter 0 of constructor in *Service required a bean of type '*Repository' that could not be found “.service”中构造函数的参数 0 需要找不到类型为“.model”的 bean - Parameter 0 of constructor in '.service ' required a bean of type '.model ' that could not be found ... 中构造函数的参数 5 需要找不到类型为“...Mapper”的 bean - Parameter 5 of constructor in … required a bean of type '…Mapper' that could not be found 构造函数的参数 0 需要一个找不到的 bean - Parameter 0 of constructor required a bean that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM