使用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. adt-bundle-windows-x86_64-20130522.zip 下载

    直接复制这个链接到迅雷下载即可:http://dl.google.com/android/adt/adt-bundle-windows-x86_64-20130522.zip

  2. Java进阶06 容器

    Java中有一些对象被称为容器(container).容器中可以包含多个对象,每个对象称为容器中的一个元素.容器是用对象封装的数据结构(data structure). 充满梦想的容器 不同的数据结构 ...

  3. json 文件解析与应用

    第一步:首先弄一个 json 文件   我这里成为 config.json 内容如下 { ": { , "desc":"中华人民共和国" }, &qu ...

  4. 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...

  5. php实现多文件上传和下载。

    http://1229363.blog.163.com/blog/static/19743427200751291055264/

  6. Smooks:xml-to-java

    引言: Smooks是一个开源的Java框架,用于处理“数据事件流”.它常常被认为是一个转换框架并以此被用于好几个产品和项目中,包括JBoss ESB(以及其它ESB).然而究其核心,Smooks未提 ...

  7. VC++6.0注释快捷键的添加使用

    在eclipse等编辑工具中都有注释代码的快捷键,但是vc++6.0没有. vc++是以VB为脚本来控制的,在网上找到了一个VB的脚本供大家使用.   工具/原料 VC++6.0 方法/步骤 打开VC ...

  8. Mesos提交任务没有被执行

    当通过marathon提交了一个任务后,发现一直处于waiting状态: 回到mesos,执行MASTER=$(mesos-resolve `cat /etc/mesos/zk`) &  me ...

  9. Java 的 Tuple 元组数据类型

    元组类型,即 Tuple 常在脚本语言中出现,例如 Scala 的 ("Unmi", "china@qq.com", "blahbla"). ...

  10. sql中in和exist语句的区别?(补充了left join和right join)

    in和exists(摘录自百度)in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 如果两个表中一个较小,一个是大表,则子查询表大的用exi ...