简体   繁体   English

通过字段“ freemarkerConfig”表示的不满意依赖性

[英]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.

相关问题 通过字段“ sv”表示的不满意依赖性 - Unsatisfied dependency expressed through field 'sv' 通过“ employeeDao”字段表示不满意的依赖关系; - Unsatisfied dependency expressed through field 'employeeDao'; 通过“ productDao”字段表示的不满意依赖性 - Unsatisfied dependency expressed through field 'productDao' 通过字段“ clientRepository”表示不满意的依赖关系; - Unsatisfied dependency expressed through field 'clientRepository'; 获取:通过字段“ userRepository”表达的不满意的依赖性 - Getting: Unsatisfied dependency expressed through field 'userRepository' 通过字段和映射问题表达的不满意依赖性 - Unsatisfied dependency expressed through field and mapping issues 通过字段“springSecurityFilterChain”表达的不满意依赖 - Unsatisfied dependency expressed through field 'springSecurityFilterChain' Spring web应用+hibernate问题:不满足的依赖通过字段表达 - Spring web application + hibernate problem: Unsatisfied dependency expressed through field 初始化SolrRepository时出错:通过字段表示的不满意依赖性 - Error while initialising SolrRepository: Unsatisfied dependency expressed through field JAVA:创建带有名称的 bean 时出错; 通过字段表达的不满意依赖 - JAVA: Error creating bean with name; Unsatisfied dependency expressed through field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM