【发布时间】: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