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

时间:2019-01-04 08:47:07

标签: java mysql spring-boot jwt

我在 Spring boot rest 中使用JWT进行API身份验证功能,但是遇到以下错误:

  

说明

     

构造函数的参数0   com.icfer.huxy.icfer.controller.UserController需要一个类型为bean的bean   'com.icfer.huxy.icfer.service.ApplicationUserRepository'   找不到。

     

操作

     

考虑定义一个类型的bean   您的“ com.icfer.huxy.icfer.service.ApplicationUserRepository”   配置。

以下是我针对ApplicationUserRepositoryUserController类实现的内容。

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

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

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

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

以下是我的项目的链接: Github link

1 个答案:

答案 0 :(得分:1)

共享实际样本的另一个原因是有帮助的,因为您共享的代码都没有提供我们需要帮助的信息。

在您的@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)

另外,请不要将EnableAutoConfigurationSpringBootApplication一起使用,后者也有exclude属性。

一旦我删除了DataSourceAutoConfiguration上的排除项,该错误就消失了。