类b中构造函数的java参数0需要找不到类a类型的bean

ohtdti5x  于 2021-06-16  发布在  Mysql
关注(0)|答案(1)|浏览(433)

我正在使用spring boot rest中的jwt创建api身份验证功能,但面临以下错误:
说明:
com.icfer.huxy.icfer.controller.usercontroller中构造函数的参数0需要找不到类型为“com.icfer.huxy.icfer.service.applicationuserrepository”的bean。
行动:
考虑在配置中定义类型为“com.icfer.huxy.icfer.service.applicationuserrepository”的bean。
下面是我所实施的关于 ApplicationUserRepository ,和 UserController 班级。
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);
}

应用程序用户.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;
    }
}

用户控制器.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);
    }
}

我的毕业档案如下:

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')
}

下面是我的项目链接:github link

3htmauhk

3htmauhk1#

另一个原因是共享一个实际的示例是有帮助的,因为您共享的代码都没有提供我们需要帮助的信息。
在你的 @SpringBootApplication 您具有以下功能:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class})
public class IcferApplication { ... }

因为你排除了 DataSourceAutoConfiguration ,否 DataSource 已创建。根本没有 DataSource 所以jpa没有起作用。没有启动jpa,因此没有创建jpa存储库。
你可以在调试模式下运行你的应用程序(例如,通过添加 -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)

另外,请不要使用 EnableAutoConfiguration 一起 SpringBootApplication ,有一个 exclude 也将其归因于后者。
一旦我移除了 DataSourceAutoConfiguration 那个错误消失了。

相关问题