【问题标题】:Error init SpringBoot v.2.1.10.RELEASE app Parameter 0 of constructor错误初始化 SpringBoot v.2.1.10.RELEASE app 构造函数参数 0
【发布时间】:2019-11-17 21:09:17
【问题描述】:

我有一个带有这个配置文件的 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);
        }

    }

还有这个控制器

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.

【问题讨论】:

  • 能否为每个类添加包语句

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


【解决方案1】:

@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"})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多