问题
I trying to use Freemarker Templating Engine to send html formatted emails. I had to do this in several services. In one service, freemarker is working fine, but not in another one. But the code, relevant for freemarker, is actually the same. The autowiring of
@Autowired
private Configuration freeMarkerConfig;
is not working in one service, but in another. The autowiring appears in an hierachy (e.g. controller autowires service, service autowires a mailservice, mailservice autowires another component, which autowires freemarker... I cant figure out how to solve this. The error stacktrace:
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userController':
Unsatisfied dependency expressed through field 'userService':
Error creating bean with name 'userService':
Unsatisfied dependency expressed through field 'mailService':
Error creating bean with name 'mailServiceImpl':
Unsatisfied dependency expressed through field 'mailMessages':
Error creating bean with name 'mailMessages':
Unsatisfied dependency expressed through field 'freeMarkerConfig':
No qualifying bean of type [freemarker.template.Configuration] found for dependency [freemarker.template.Configuration]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [freemarker.template.Configuration] found for dependency [freemarker.template.Configuration]: expected at least 1 bean which qualifies as autowire candidate for this dependency.
As I am using spring-boot 1.4.0, there's no need to provide more configuration, or am I wrong ? So i dont have any further xml nor java configuration for freemarker.. I didnt configured it, as there was no need to configure anything in other services, where I use freemarker.
回答1:
As I am using spring-boot 1.4.0, there's no need to provide more configuration, or am I wrong ? So i dont have any further xml nor java configuration for freemarker.. I didnt configured it, as there was no need to configure anything in other services, where I use freemarker.
Unless you are using a starter project that automatically sets up a bean that configures the Freemarker engine, you'll need to create one. Something like this:
@Configuration
public class MyConfiguration {
@Bean
freemarker.template.Configuration freeMarkerConfig() {
return someConfigBeanInstantiatedHere;
}
}
This looks like a pretty good reference for a starter project that sets up Freemarker for you.
来源:https://stackoverflow.com/questions/40107179/autowiring-of-freemarker-configuration-fails