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翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...
随机推荐
- FFMPEG相关开源项目
1.FFmpeg build for android random architectures with example jnihttps://github.com/appunite/AndroidF ...
- bzoj1000~1025
以后还是这样 25道题一起发 看着爽 noip失利之后发粪涂墙 刷了一波bzoj 题解: bzoj1000 A+B问题 这题不同的人有不同的写法,我写了个线段树套Treap,应该还是挺简单的 但是看别 ...
- Solaris/Linux 命令手册
无意翻到之前收藏的一个文档,共享一下. Solaris/Linux 命令手册 1. 系统 # passwd:修改口令 # exit:退出系统 2. 文件 # cp:复制文件或目录,参数:-a递归目录, ...
- ACM学习历程—HDU4675 GCD of Sequence(莫比乌斯)
Description Alice is playing a game with Bob. Alice shows N integers a 1, a 2, …, a N, and M, K. She ...
- 4445: [Scoi2015]小凸想跑步 半平面交
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4445 题解: 设点坐标,利用叉积可以解出当p坐标为\((x_p,y_p)\)时,与边i- ...
- [转]CSS3盒模型display:box详解
时间:2014-02-25来源:网络作者:未知编辑:RGB display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布 ...
- LAMP 1.4 PHP编译安装问题解决
环境:centos X64 最小化安装 php版本:php-5.4.3 安装前.先安装些软件和库文件 yum install -y gcc gcc-c++ make zlib zlib-devel p ...
- console (控制台)
console 模块提供了一个简单的调试控制台,类似于 Web 浏览器提供的 JavaScript 控制台. 该模块导出了两个特定的组件: 一个 Console 类,包含 console.log() ...
- assert.ifError()
assert.ifError(value) 如果 value 为真,则抛出 value. 可用于测试回调函数的 error 参数(通俗解释ifError方法断定某个表达式是否false,如果该表达式对 ...
- 树莓派 Learning 002 装机后的必要操作 --- 01 解决上网问题
树莓派 装机后的必要操作 - 解决上网问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 装机后的必要操作 解决上网问题 解决上网 ...