我的邮件服务有构造函数参数,并实现了一个接口
public class MailServiceImpl implements MailService {
private MailBo mail;
public MailBO getMail() {
return Mail;
}
public void setMail(MailBO Mail) {
this.Mail = Mail;
}
public MailServiceImpl(MailBo mail) {
throw exception if from and to address is not here
this.mail = mail;
}
public void sendMail(){
use mail object to send mail here
}
}
所以现在我正在尝试编写一个测试,我想用它传递的构造函数参数创建自动装配的Mailserviceimpl实例。
我跟随this链接到构造函数的autowire对象,但我得到了异常
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.model.MailBO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我的考试班
public class MailServiceImplTest {
@Autowired
private MailService mailServiceImpl;
@Test
public void testSendValidMail(){
//test
}
}
编辑: -
添加春天背景
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.service" />
</beans>
编辑2: - 我希望我的mailbo加载一些值,并作为构造函数对象注入mailserviceimpl
答案 0 :(得分:1)
我认为MailServiceImpl
和MailBO
是Spring Beans。然后只需通过@Autowired
注释MailServiceImpl构造函数,Spring将自动注入构造函数依赖