我正在编写一个发送电子邮件的Java Spring应用程序。我正在从 SQL 数据库中检索数据(电子邮件地址(。我导入了我需要的数据库存储库,但 Spring 无法注入该存储库。我的邮件系列如下所示:
@Service
public class MailService {
private final Logger log = LoggerFactory.getLogger(MailService.class);
private final static String USER = "user";
private final String baseUrl;
private final ApplicationProperties applicationProperties;
private final JavaMailSenderImpl javaMailSender;
private final MessageSource messageSource;
private final SpringTemplateEngine templateEngine;
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
public MailService(ApplicationProperties applicationProperties, JavaMailSenderImpl javaMailSender,
MessageSource messageSource, SpringTemplateEngine templateEngine) {
this.applicationProperties = applicationProperties;
this.javaMailSender = javaMailSender;
this.messageSource = messageSource;
this.templateEngine = templateEngine;
baseUrl = applicationProperties.getBaseUrl();
javaMailSender.setPort(applicationProperties.getPort());
javaMailSender.setHost(applicationProperties.getHostName());
}
我得到的错误是:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setUserRepository in com.test.mail.service.MailService required a bean of type 'com.test.repository.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.test.repository.UserRepository' in your configuration.
有谁知道这可能是什么?谢谢。
正如评论中所说的那样:应该添加@ComponentScan
。 例如:
@ComponentScan(basePackages = "com.test.repository")
@Configuration
public class SpringComponentScanApp {