[英]Unsatisfied dependency expressed through field 'freemarkerConfig'
I want to send e-mails using Apache Freemaker I tried this: 我想使用Apache Freemaker发送电子邮件,我尝试这样做:
@Service
public class EMailSender {
@Autowired
private Configuration freemarkerConfig;
public void sendMail(String to, String subject, String content) {
try { freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/templates");
EmailRegistrationModel obj = new EmailRegistrationModel();
obj.setMailSubject("Test");
Map<String, Object> model = new HashMap<String, Object>();
model.put("title", "Some name");
obj.setModel(model);
String data = geFreeMarkerTemplateContent(model);
helper.setText(data, true);
mailSender.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
private String geFreeMarkerTemplateContent(Map<String, Object> model){
StringBuffer content = new StringBuffer();
try{
content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
freemarkerConfig.getTemplate("emails_activate.html"), model));
return content.toString();
}catch(Exception e){
System.out.println("Exception occured while processing fmtemplate:"+e.getMessage());
}
return "";
}
}
Object for configuration: 配置对象:
public class EmailRegistrationModel {
private String mailContent;
private Map<String, Object> model;
....
}
When I deploy the code I get: 当我部署代码时,我得到:
Error creating bean with name 'EMailSender': Unsatisfied dependency expressed through field 'freemarkerConfig'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'freemarker.template.Configuration' available: expected single matching bean but found 2: getFreeMarkerConfiguration,freeMarkerConfiguration
Do you know how I can solve this issue? 你知道我该怎么解决吗? I suppose that I need to add some freemarker configuration?
我想我需要添加一些freemarker配置吗? Can you give me some advice?
你能给我一些建议吗?
The problem is not that you have to few Freemarker configs but two much. 问题不在于您没有几个Freemarker配置,而是两个。 Pay special attention to the last part of the exception message:
请特别注意异常消息的最后一部分:
No qualifying bean of type 'freemarker.template.Configuration' available: expected single matching bean but found 2 : getFreeMarkerConfiguration,freeMarkerConfiguration
没有可用的类型为'freemarker.template.Configuration'的合格Bean: 预期为单个匹配的Bean,但找到了2 :getFreeMarkerConfiguration,freeMarkerConfiguration
Spring Boot already comes with a FreeMarkerAutoConfiguration
. Spring Boot已经带有
FreeMarkerAutoConfiguration
。 Most probably you come with another one which you defined manually, could you verify this? 您很可能会附带另一个您手动定义的工具,您能验证一下吗?
Please stick to the Freemarker section in the application.properties or in other words: configure you application with the spring.freemarker.*
properties of Spring Boot. 请坚持application.properties中的Freemarker部分,或者换句话说:使用Spring Boot的
spring.freemarker.*
属性配置您的应用程序。
expected single matching bean but found 2: getFreeMarkerConfiguration,freeMarkerConfiguration
There may be a situation when you create more than one bean of the same type and want to wire only one of them with a property. 当您创建多个同类型的bean,并且只想将其中一个与属性连接时,可能会出现这种情况。 In such cases, the above error comes.
在这种情况下,会出现上述错误。 Why this happened?
为什么会这样?
Solution 1: Use
@Resource
instead of@Autowired
Refer This解决方案1:使用
@Resource
代替@Autowired
请参阅此
Solution 2: Use
@Qualifier
Refer This解决方案2:使用
@Qualifier
引用此
Solution 3: Use
@Primaray
Refer This解决方案3:使用
@Primaray
请参阅此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.