【问题标题】:Unsatisfied dependency expressed through constructor parameter 1 with userRepository通过带有 userRepository 的构造函数参数 1 表示的不满足的依赖关系
【发布时间】:2019-11-09 18:34:57
【问题描述】:

在响应式并使用 jwt 的 Spring Boot 应用程序中,在我的 spring-cloud-gateway 中,我有这段代码。

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    }

}

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SpringSecurityWebFluxConfig {

    private final UserServiceImpl userService;
    private final JwtTokenUtil tokenUtil;

    private static final String[] AUTH_WHITELIST = {
        "/resources/**",
        "/webjars/**",
        "/authorize/**",
        "/favicon.ico"};


    public SpringSecurityWebFluxConfig(JwtTokenUtil tokenUtil, UserServiceImpl userService) {
        this.tokenUtil = tokenUtil;
        this.userService = userService;
    }
    ..
}

@Service
public class UserServiceImpl implements ReactiveUserDetailsService, UserService {

    private final UserRepository userRepository;

    public UserServiceImpl(final UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    ...

}

@Repository
public interface UserRepository extends ReactiveCrudRepository<User, Integer>{
}

public class JWTHeadersExchangeMatcher implements ServerWebExchangeMatcher {
    @Override
    public Mono<MatchResult> matches(final ServerWebExchange exchange) {
    }
}

public class JWTReactiveAuthenticationManager implements ReactiveAuthenticationManager {
    ...

    public JWTReactiveAuthenticationManager(final PasswordEncoder passwordEncoder, final UserServiceImpl userService) {
        this.passwordEncoder = passwordEncoder;
        this.userService = userService;
    }
}


public class JwtTokenUtil {
    ...
}

public class TokenAuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
     private final JwtTokenUtil tokenProvider;

    public TokenAuthenticationConverter(JwtTokenUtil tokenProvider) {
        this.tokenProvider = tokenProvider;
    }
}

public class TokenAuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
     private final JwtTokenUtil tokenProvider;

    public TokenAuthenticationConverter(JwtTokenUtil tokenProvider) {
        this.tokenProvider = tokenProvider;
    }
}
  • [main] onfigReactiveWebServerApplicationContext : 上下文初始化期间遇到异常 - 取消 刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为“springSecurityWebFluxConfig”的 bean 时出错 文件 [/home/mac/Development/project/reactive-cloud/gateway-service/build/classes/java/main/com/example/gatewayservice/config/SpringSecurityWebFluxConfig.class]: 通过构造函数参数1表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建文件中定义的名称为“userServiceImpl”的 bean 时出错 [/home/mac/Development/project/reactive-cloud/gateway-service/build/classes/java/main/com/example/gatewayservice/service/UserServiceImpl.class]: 通过构造函数参数 0 表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 符合条件的 bean 类型 'com.example.gatewayservice.repository.UserRepository' 可用: 预计至少有 1 个 bean 有资格作为 autowire 候选者。 依赖注释:{} 2019-06-27 11:47:23.336 INFO 53073 --- [
    main] ConditionEvaluationReportLoggingListener :

试图把自动连线

关于 SpringSecurityWebFluxConfig 类中的 UserServiceImpl
关于 UserServiceImpl 类中的 UserRepository
关于 JWTReactiveAuthenticationManager.class 中的 UserServiceImpl

但得到同样的错误

编辑

如果我使用

@EnableR2dbcRepositories 到 GatewayServiceApplication,我没有这个错误,但它搜索 DatabaseClient

【问题讨论】:

    标签: spring spring-boot spring-cloud-gateway r2dbc spring-data-r2dbc


    【解决方案1】:

    请在 UserServiceImpl 类中为 UserRepository 变量在构造构造函数或变量级别添加 @Autowired 注解。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2017-01-01
    • 2021-12-10
    相关资源
    最近更新 更多