我有一个使用Spring Boot + FreeMarker构建的项目,它在今晚工作正常,但我认为我没有改变任何东西;但它失败了。下面是我的FreeMarker配置类:
@Configuration
@Slf4j
public class FreemarkerConfiguration extends FreeMarkerAutoConfiguration.FreeMarkerWebConfiguration {
/**
* autowired all implementations of freemarker.template.TemplateDirectiveModel
*/
@Autowired
Map<String, TemplateDirectiveModel> directiveModelMap;
/**
* autowired all implementations of freemarker.template.TemplateMethodModelEx
*/
@Autowired
Map<String, TemplateMethodModelEx> methodModelExMap;
private static final String CUSTOM_DIRECTIVE_SUFFIX = "Directive";
private static final String CUSTOM_METHOD_SUFFIX = "Method";
@Override
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = super.freeMarkerConfigurer();
Map<String, Object> sharedVariables = new HashMap<String, Object>();
if (!CollectionUtils.isEmpty(directiveModelMap)) {
Map<String, Object> map = new HashMap<String, Object>();
for (Map.Entry<String, TemplateDirectiveModel> entry : directiveModelMap.entrySet()) {
map.put(StringUtils.uncapitalize(entry.getKey()).replaceAll(CUSTOM_DIRECTIVE_SUFFIX, ""), entry.getValue());
}
sharedVariables.putAll(map);
}
if (!CollectionUtils.isEmpty(this.methodModelExMap)) {
Map<String, Object> map = new HashMap<String, Object>();
for (Map.Entry<String, TemplateMethodModelEx> entry : this.methodModelExMap.entrySet()) {
map.put(StringUtils.uncapitalize(entry.getKey()).replaceAll(CUSTOM_METHOD_SUFFIX, ""), entry.getValue());
}
sharedVariables.putAll(map);
}
BeansWrapper beansWrapper = new BeansWrapperBuilder(freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build();
sharedVariables.put("enums", beansWrapper.getEnumModels());
configurer.setFreemarkerVariables(sharedVariables);
return configurer;
}
}
问题在于
@Autowired
Map<String, TemplateDirectiveModel> directiveModelMap;
@Autowired
Map<String, TemplateMethodModelEx> methodModelExMap;
我想注入TemplateDirectiveModel和TemplateMethodModelEx的所有实现,但Map<String ,TemplateDirectiveModel/TemplateMethodModelEx>
都获得了null
。当然,使用@Compoment
注释的实现。我不知道为什么,我比较差异但没有得到答案,为什么Maps
在
@Override
public FreeMarkerConfigurer freeMarkerConfigurer(){ .... }
这是我的boot application
@Configuration
@SpringBootApplication
@EntityScan("com.hmxx.entity")
@EnableAspectJAutoProxy
@EnableTransactionManagement
@EnableJpaRepositories(value = {"com.hmxx.service"})
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(new Object[]{Application.class});
app.setWebEnvironment(true);
//app.setBannerMode(Banner.Mode.CONSOLE);
ConfigurableApplicationContext ctx = app.run(args);
Map<String, TemplateDirectiveModel> directiveModelMap = ctx.getBeansOfType(TemplateDirectiveModel.class);
Map<String, TemplateMethodModelEx> methodModelExMap = ctx.getBeansOfType(TemplateMethodModelEx.class);
}
@Autowired
DataInitService dataInitService;
@Override
public void run(String... args) throws Exception {
// dataInitService.initAdminUser();
}
}
显然Map<String, TemplateDirectiveModel>、Map<String, TemplateMethodModelEx> methodModelExMap
都不为空。
我想知道为什么注射得到null
并希望解决它。