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翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...
随机推荐
- 利用Perlin nosie 完成(PS 滤镜—— 分成云彩)
%%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...
- urllib,urlib2与httplib,urllib3
urllib:编码参数离不开urllib,urllib.urlencode, urllib.urlopen(URL,[,data]) 支持POST,根据参数区分post或者get urllib2:发送 ...
- fedora使用mac osx字体和渲染方式
fedora 19的倒退(中文显示有问题)让人感到很沮丧,不过,后来还是找到了一个很好的解决方案:使用max osx的字体和渲染方式 1. 安装infinality字体渲染软件: rpm -Uvh h ...
- 洛谷 P1767 家族_NOI导刊2010普及(10)
题目描述 在一个与世隔绝的岛屿上,有一个有趣的现象:同一个家族的人家总是相邻的(这里的相邻是指东南西北四个方向),不同的家族之间总会有河流或是山丘隔绝,但同一个家族的人不一定有相同姓氏.现在给你岛上的 ...
- 洛谷【P3379】【模板】最近公共祖先(LCA)
浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html 题目传送门:https://www.luogu.org/problemnew/show/ ...
- linux python 更新版本
更新python: 第1步:更新gcc,因为gcc版本太老会导致新版本python包编译不成功 代码如下: #yum -y install gcc 系统会自动下载并安装或更新,等它自己结束 第2步:下 ...
- 微服务理论之五:微服务架构 vs. SOA架构
一.面向服务的架构SOA 面向服务的架构是一种软件体系结构,应用程序的不同组件通过网络上的通信协议向其他组件提供服务.通信可以是简单的数据传递,也可以是两个或多个服务彼此协调连接.这些独特的服务执行一 ...
- SVD实例
>> X = rand(5,7) X = 0.9797 0.1365 0.6614 0.5828 0.2259 0.2091 0.5678 0.2714 0.0118 0.2844 0.4 ...
- Tomcat 服务器详解
工具/原料 1.JDK:版本为jdk-7-windows-i586.exe 下载地址 http://www.oracle.com/technetwork/java/javase/download ...
- Web Form要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。”的解决办法。
1.先将aspnet.scriptmanager.jquery.dl 复制到bin (网站根目录下的bin文件夹找不到,看看下面的图片中点击[显示所有文档]) 文件夹下. 2.在网站根目录下s ...