使用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的更多相关文章

  1. springboot 简单自定义starter - dubbo

    首先需要引入pom 这里使用nacos注册中心 所以引入了nacos-client 使用zookeeper注册中心的话需要引入其相应的client <dependency> <gro ...

  2. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  3. SpringBoot编写自定义Starter

    根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库 1.建 ...

  4. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

  5. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  6. SpringBoot第十六篇:自定义starter

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   这一段时间 ...

  7. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  8. SpringBoot Starter机制 - 自定义Starter

    目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言         最近在学习Sp ...

  9. SpringBoot系列之自定义starter实践教程

    SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...

随机推荐

  1. OpenCV——百叶窗

    参考: PS 图像特效,百叶窗 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLU ...

  2. 【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. 【leetcode刷题笔记】Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  4. thinkphp中图片上传的几种好的办法

    http://www.thinkphp.cn/code/701.html http://www.thinkphp.cn/code/151.html

  5. Hihocoder1662 : 查找三阶幻方([Offer收割]编程练习赛40)(暴力)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N x M的矩阵,请你数一数其中有多少个3 x 3的子矩阵可以构成三阶幻方? 如果3 x 3的矩阵中每一行.每一列 ...

  6. PLSQL Developer安装、配置、连接oracle数据库

    0.资源准备 1) PLSQL Developer安装包(由于安装包超过10M,无法上传,请自行下载) 2) instantclient_11_2安装包(由于安装包超过10M,无法上传,请自行下载) ...

  7. [转]WebKit CSS3 动画基础

    前几天在Qzone上看到css3动画,非常神奇,所以也学习了一下.首先看看效果http://www.css88.com/demo/css3_Animation/ 很悲剧的是css3动画现在只有WebK ...

  8. django基础PROJECT APP View template

    project 和 app 的区别就是一个是配置另一个是代码: 一个project包含很多个Django app以及对它们的配置. 一个project的作用是提供配置文件,比方说哪里定义数据库连接信息 ...

  9. 文件异步上传,多文件上传插件uploadify

    本文中使用java作为例子 uploadify下载 http://files.cnblogs.com/chyg/uploadify.zip jsp页面中需要引入: <script type=&q ...

  10. hive查询ncdc天气数据

    使用hive查询ncdc天气数据 在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果. 1. 在hive中创建ncdc表,这个 ...