在构建beetl模板引擎时,使用自定义的加载器:自定义加载器使用方法
@Autowired
MongoResourceLoader mongo;
String PREFIX_MONGO = "mongo:";
......
// 使用组合加载器
CompositeResourceLoader resourceLoader = new CompositeResourceLoader();
resourceLoader.addResourceLoader(new StartsWithMatcher(BeetlConstants.PREFIX_MONGO), mongo);
// 使用默认配置
Configuration cfg;
try {
cfg = Configuration.defaultConfiguration();
} catch (IOException e) {
throw new RuntimeException(WallMessageConstants.INIT_BEETL_FAILED);
}
// 构建Beetl核心类,建议使用单例方式构建
groupTemplate = new GroupTemplate(resourceLoader, cfg);
MongoResourceLoader代码:关键方法
@Component
@Slf4j
public class MongoResourceLoader implements ResourceLoader {
@Autowired
MongoTemplateService mongoTemplateService;
@Override
public Resource getResource(String uniqueId) {
return new MongoResource(mongoTemplateService.getTemplateConfig(uniqueId), this);
}
......
}
/**
* 配置由mongo控制
*
* @author TWH 2020/8/12 15:20
*/
@Service
@Slf4j
public class MongoTemplateService implements InitializingBean {
@Autowired
TailgateTemplateDao templateDao;
@Autowired
TailgateTemplateContentDao templateContentDao;
@Getter
HashMap<String, AppInfo> appInfoHashMap = new HashMap<>();
/**
* 模板内容本地缓存
* 格式:Map<Api_TemplateId,ApiTemplateContent>
*/
private static HashMap<String, ApiTemplateContent> templateContentHashMap = new HashMap<>();
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void afterPropertiesSet() {
log.info("启用MongoDb作为模板存储器!");
List<TemplateInfo> templateInfoList = templateDao.findAll();
// 通过挡板配置构建映射关系,形成最终的appInfo集合
templateInfoList.forEach(item -> {
Map<String, ApiInfo> apis = Optional.ofNullable(appInfoHashMap.get(item.getAppId()))
.orElseGet(() -> {
AppInfo appInfo = new AppInfo();
appInfo.setAppId(item.getAppId());
appInfoHashMap.put(item.getAppId(), appInfo);
return appInfo;
}).getApis();
Optional.ofNullable(apis.get(item.getApiId())).orElseGet(() -> {
ApiInfo info = new ApiInfo();
info.setAppId(item.getAppId());
info.setApiId(item.getApiId());
apis.put(item.getApiId(), info);
return info;
}).getTemplateIds().put(item.getTemplateId(), item);
});
if (log.isInfoEnabled()) {
Set<String> templateUniqueIdSet = templateInfoList.parallelStream()
.collect(Collectors.mapping(templateInfo -> templateInfo.genUniqueId(), Collectors.toSet()));
TreeSet<String> orderSet = new TreeSet<>(templateUniqueIdSet);
log.info("加载Mongo模板完成,列表【{}】", orderSet);
}
// 加载所有模板
List<ApiTemplateContent> templateContents = templateContentDao.findAll();
templateContents.forEach(apiTemplateContent ->
templateContentHashMap.put(apiTemplateContent.genUniqueId(), apiTemplateContent));
log.debug("加载所有模板内容完成【{}】",templateContentHashMap.keySet());
}
/**
* 根据templateId 获取模板
* 自动使用 ThreadLoacl中存储的appId和apiId 作为查询template的条件。
*
* @param uniqueKey 模板唯一索引
* @return 模板对象
*/
public TemplateInfo getTemplateConfig(String uniqueKey) {
TemplateInfo templateInfo = templateDao.findByUniqueId(uniqueKey);
return templateInfo;
}
public static ApiTemplateContent getTemplateContent(String apiId,String templateId){
String key = TailgateUtil.buildUniqueId(apiId,templateId);
return templateContentHashMap.get(key);
}
public String addOrUpdateConfig(TemplateInfo templateInfo) {
// 构建ID值 用于save方法控制id
templateInfo.genUniqueId();
templateDao.save(templateInfo);
applicationEventPublisher.publishEvent(new MongoTemplateRefreshEvent(this, templateInfo));
return "OK";
}
public void deleteConfig(TemplateInfo templateInfo) {
templateInfo.genUniqueId();
templateDao.delete(templateInfo);
}
public String addOrUpdateConfig(ApiTemplateContent apiTemplateContent) {
// 构建ID值 用于save方法控制id
apiTemplateContent.genUniqueId();
templateContentDao.save(apiTemplateContent);
return "OK";
}
public void deleteConfig(ApiTemplateContent apiTemplateContent) {
apiTemplateContent.genUniqueId();
templateContentDao.delete(apiTemplateContent);
}
有了这些之后,在使用时就可以直接使用 mongo: 模板ID 完成模板使用了!