java自定义配置文件_自定义配置文件如何配置
@Component
public?class?RoleDirectiveModel?implements?TemplateDirectiveModel?{
/**
*
*?@param?environment?環境變量
*?@param?params?指令參數(存儲你所需要的值,隨便是什么Key-Value你懂的)
*?@param?loopVars?循環變量?返回值
*?@param?templateDirectiveBody?指令內容
*?除了params外,其他的都能是Null。
*?@throws?TemplateException
*?@throws?IOException
*/
@Override
public?void?execute(Environment?environment,?Map?params,?TemplateModel[]?loopVars,?TemplateDirectiveBody?templateDirectiveBody)?throws?TemplateException,?IOException?{
System.out.println("=========");
TemplateScalarModel?user=(TemplateScalarModel)params.get("user");
TemplateScalarModel?role=(TemplateScalarModel)params.get("role");
if("123456".equals(user.getAsString())?&&?"admin".equals(role.getAsString())?){
loopVars[0]=TemplateBooleanModel.TRUE;
}
List?otherRights=new?ArrayList<>();
otherRights.add("add");
otherRights.add("update");
otherRights.add("delete");
loopVars[1]=new?SimpleSequence(otherRights);
templateDirectiveBody.render(environment.getOut());
}
}
這是自定義指令的定義類
這是加載自定義指令的freemarker?java配置類import?com.example.demo.model.tag.RoleDirectiveModel;
import?com.example.demo.model.tag.SortMethod;
import?freemarker.template.TemplateModelException;
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.context.annotation.Configuration;
import?javax.annotation.PostConstruct;
@Slf4j
@Configuration
public?class?FreeMarkerAutoConfiguration?{
@Autowired
private?freemarker.template.Configuration?configuration;
@Autowired
private?RoleDirectiveModel?roleDirectiveModel;
@Autowired
private?SortMethod?sortMethod;
@PostConstruct
public?void?setSharedVariable()?{
try?{
//自定義標簽
configuration.setSharedVariable("role",?roleDirectiveModel);
configuration.setSharedVariable("sort_int",sortMethod);
}?catch?(Exception?e)?{
log.error("Custom?tags?failed?to?load:{}",?e.getMessage());
}
}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java自定义配置文件_自定义配置文件如何配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java文件file字符集_获取文件字符
- 下一篇: mysql 5.1.60.tar.gz_