springboot 简单自定义starter - beetl
使用idea新建springboot项目beetl-spring-boot-starter 首先添加pom依赖
packaging要设置为jar不能设置为pom<packaging>jar</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.0.0.M1</version>
</dependency>
编写BeetlProperties
@Component
@ConfigurationProperties(prefix = "beetl")
public class BeetlProperties { /**
* 是否开启beetl 默认开启
*/
private Boolean enabled = true;
/**
* 模板根目录 默认templates
*/
private String templatesPath = "templates";
/**
* 注册的beetl全局共享变量
*/
private Map<String,Object> vars = new HashMap<>();
/**
* beetl自定义全局函数
*/
private Map<String,Class> FNP = new HashMap<>(); public Boolean isEnabled() {
return enabled;
} public void setEnabled(Boolean enabled) {
this.enabled = enabled;
} public String getTemplatesPath() {
return templatesPath;
} public void setTemplatesPath(String templatesPath) {
this.templatesPath = templatesPath;
} public Map<String, Object> getVars() {
return vars;
} public void setVars(Map<String, Object> vars) {
this.vars = vars;
} public Map<String, Class> getFNP() {
return FNP;
} public void setFNP(Map<String, Class> FNP) {
this.FNP = FNP;
} }
编写 BeetlAutoConfiguration (使用ConditionalOnProperty注解确定是否加载beetl beetl.enabled=false 的话就不加载beetl了)
@Configuration
@ConditionalOnProperty(
name = {"beetl.enabled"},
havingValue = "true",
matchIfMissing = true)
@EnableConfigurationProperties({BeetlProperties.class})
public class BeetlAutoConfiguration { @Autowired
private BeetlProperties beetlProperties; @Bean(name = "beetlConfig")
@ConditionalOnMissingBean(name={"beetlConfig"})
public BeetlGroupUtilConfiguration beetlGroupUtilConfiguration(){
BeetlGroupUtilConfiguration beetlConfig = new BeetlGroupUtilConfiguration();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader==null){
loader = BeetlAutoConfiguration.class.getClassLoader();
}
//beetlConfig.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要
ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, beetlProperties.getTemplatesPath());
beetlConfig.setResourceLoader(cploder);
beetlConfig.init();
//如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
beetlConfig.getGroupTemplate().setClassLoader(loader);
//注册全局变量
beetlConfig.getGroupTemplate().setSharedVars(beetlProperties.getVars());
//注册全局函数
for(Map.Entry<String,Class> map:beetlProperties.getFNP().entrySet()){
beetlConfig.getGroupTemplate().registerFunctionPackage(map.getKey(),map.getValue());
}
return beetlConfig;
} @Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver beetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlConfig){
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlConfig);
return beetlSpringViewResolver;
} // @Bean(name = "beetlViewResolver")
// @ConditionalOnMissingBean(name = {"beetlViewResolver"})
// public BeetlSpringViewResolver beetlSpringViewResolver(){
// BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
// beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
// beetlSpringViewResolver.setOrder(0);
// beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration());
// return beetlSpringViewResolver;
// } }
然后在resources 下面新建 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.chao.beetl.BeetlAutoConfiguration
在其他项目引入就可以使用了
springboot 简单自定义starter - beetl的更多相关文章
- springboot 简单自定义starter - dubbo
首先需要引入pom 这里使用nacos注册中心 所以引入了nacos-client 使用zookeeper注册中心的话需要引入其相应的client <dependency> <gro ...
- (springboot)自定义Starter
要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...
- SpringBoot编写自定义Starter
根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库 1.建 ...
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
- SpringBoot应用篇(一):自定义starter
一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...
- SpringBoot第十六篇:自定义starter
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 这一段时间 ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- SpringBoot Starter机制 - 自定义Starter
目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言 最近在学习Sp ...
- SpringBoot系列之自定义starter实践教程
SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...
随机推荐
- 【leetcode刷题笔记】N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】
平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...
- test20190611 NOIP模拟赛
题一:答题比赛 [问题描述] YYH报名参加了一个特殊的电视问答节目.这个节目共有n个问题,每回答正确1题,YYH就会获得1分,而每当YYH连续答对k题,那么他的现有得分乘以2,注意答对第k题后,是先 ...
- Percona Xtrabackup 备份MySQL 实例(转)
老规矩,开场白,刚开始用mysqldump,备份100G+的数据库,再加上服务器繁忙,备份速度像蜗牛似的,于是寻找更高效的备份方法.网上都说用xtrabackup比较适合备份大的数据库,而且备份效率也 ...
- sql查询将列里面的值替换为别的值但是实际值不变
数据库有一张表BUG(缺陷记录表) 里面有字段severity(严重程度): severity的值实际为1,2,3,4,但希望在查询结果中将severity的1,2,3,4值显示为其他的值,但seve ...
- Selenium并行启动多个浏览器
如果你对 Selenium 自动化测试已经非常熟悉,你仅仅需要一个快速上手来使程序运行起来.本章节的内容能满足不同的技术层次,但是如果你仅仅需要一个可以快速上手的指引,那么就显得有点多.如果是这样,你 ...
- qboimathtest1 t1 魔法串
题目 小 N 最近在沉迷数学问题. 对于一个数字串 S,如果可以将它划分成两个数字 A.B,满足: 1. S=AB. 2. A.B 均不包含前导 0. 3. B 是 A 的倍数,且B / A是完全立方 ...
- winDump
windump -i 00-00-10-00-43-A2 监听网卡(一个适配器一个网卡,一个mac)
- Struts学习第一课 使用Filter作为控制器的MVC应用
MVC设计模式概览 实现MVC(Model,View,Controller)模式的应用程序由3大部分构成: -模型:封装应用程序的数据和业务逻辑(POJO,Plain Old Java Object) ...
- 一个web.Config或app.Config自定义段configSections的示例--转
直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...