java - 错误 init SpringBoot v.2.1.10.RELEASE app 构造函数参数 0

标签 java spring spring-boot spring-mvc dependency-injection

我有一个带有此配置文件的 SpringBoot 应用程序:

package com.bonanza.web.config;
    @Configuration
    @EnableJpaRepositories(basePackages = "com.bonanza.backend.repository")
    @EntityScan(basePackages = "com.bonanza.backend")
    @EnableTransactionManagement
    @EnableCaching
    @PropertySource("file:///${user.home}/.bonanza/application-common.properties")
    public class BonanzaApplicationConfig {

    }

以及此服务:

package com.bonanza.backend.service;


    @Service
    @Transactional(
        readOnly = true
    )
    public class UserService {

        private final RoleRepository roleRepository;
        private final UserRepository userRepository;
        private final PasswordEncoder passwordEncoder;
        private final PasswordResetTokenRepository passwordResetTokenRepository;

        public UserService(RoleRepository roleRepository, UserRepository userRepository, PasswordEncoder passwordEncoder, PasswordResetTokenRepository passwordResetTokenRepository) {
            this.roleRepository = roleRepository;
            this.userRepository = userRepository;
            this.passwordEncoder = passwordEncoder;
            this.passwordResetTokenRepository = passwordResetTokenRepository;
        }
    ..
    }

和主类:

package com.bonanza.web
    @SpringBootApplication
    public class BonanzaWebApplication {

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

    }

和这个 Controller

package com.bonanza.web.controller;
        @Controller
        public class AppErrorController implements ErrorController {
        protected final UserService userService;
        ..

    public AppErrorController(UserService userService, ErrorAttributes errorAttributes, EmailService emailService) {
            super(userService);
            this.errorAttributes = errorAttributes;
            this.emailService = emailService;
        }
    ...
        }

但是当我启动应用程序时。我有这个错误:

Description:

Parameter 0 of constructor in com.bonanza.web.controller.AppErrorController required a bean of type 'com.bonanza.backend.service.UserService' that could not be found.


Action:

Consider defining a bean of type 'com.bonanza.backend.service.UserService' in your configuration.

最佳答案

@SpringBootApplication ,只会搜索当前包及其所有子包中的组件/bean。您的UserService

com.bonanza.backend.service

不是 BonanzaWebApplication 的子包

com.bonanza.web

这样就可以与所有需要组件扫描的包一起使用

@ComponentScan({"com.bonanza.web","com.bonanza.backend.service"})
@SpringBootApplication
public class BonanzaWebApplication {

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

}

您还可以在 @SpringBootApplication 中指定组件扫描自己注释

@SpringBootApplication(scanBasePackages = {"com.bonanza.web","com.bonanza.backend.service"})

关于java - 错误 init SpringBoot v.2.1.10.RELEASE app 构造函数参数 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905222/

相关文章:

java - 为什么在gradle中有两种创建任务的方式?

java - TableModelListener 并不总是工作

java - Spring CustomAuthenticationProviderauthentication.getName() 返回空字符串

java - 将 HTTP 请求数据转换为枚举时捕获异常

java - Oracle 序列 - 'increment by' 和 'cache'

spring-security - 启用 Spring Security 使 Swagger 输出文本/纯文本而不是 HTML

java - groovy中的invokeMethod是在哪里实现的?

java - 如何使用 javafx.beans.binding.Bindings.select(...) 进行简洁的值绑定(bind)

java - 如何模拟 RestTemplate

java - Junit 测试单独通过,而不是分组通过