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 {@Autowiredprivate 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
在其他项目引入就可以使用了
转载于:https://www.cnblogs.com/rchao/p/10917256.html
springboot 简单自定义starter - beetl相关推荐
- SpringBoot - 开发自定义starter
一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...
- springboot框架学习 - 自定义 starter
本篇主要演示 springboot 中自定义 starter 的实例,不涉及底层原理. 首先,创建一个什么都没有的工程作为父工程,这个工程不需要进行任何操作,然后创建两个模块,分别命名为 tyu-he ...
- @configurationproperties注解的使用_徒手使用SpringBoot自定义Starter启动器
前言 在使用SpringBoot框架时,SpringBoot 最强大的功能就是把我们常用的场景抽取封装成了一个个starter,将其称之为场景启动器.搭建项目时,通过引入SpringBoot为我提供的 ...
- 玩转 SpringBoot 2.x 之自定义Starter依赖
前言 SpringBoot 核心功能之一就是 starter 依赖也叫起步依赖,他默认提供了 spring-boot-starter-web.spring-boot-starter-logging.s ...
- 根据自动装配原理在Springboot项目中自定义starter,并实现热插拔技术,自定义@enable
根据自动装配原理在Springboot项目中自定义starter,并实现热插拔技术 自定义starter 简单步骤 使用测试 优化(热插拔技术) 自定义starter 简单步骤 创建项目,并引入aut ...
- SpringBoot自定义Starter(自动配置类)
前言 SpringBoot其实从诞生以来围绕的核心就是快速构建项目,快速构建的前提是有人帮你做好轮子,开发者只要拿来即用就好了,而造好轮子的人就是SpringBoot的开发者,引入自动配置的形式帮助开 ...
- SpringBoot项目为什么需要引入大量的starter?如何自定义starter
为什么我们在使用SpringBoot框架开发Java Web应用需要引入大量的starter?例如,我们引入Redis就在Maven中导入 spring-boot-starter-data-redis ...
- SpringBoot 自定义Starter(阿里云短信、消息推送)
首先在IDEA中创建SpringBoot项目,引入相关必要依赖,本次以阿里云短信/消息推送为例: <dependency><groupId>com.aliyun</gro ...
- 【读官方文档,学原味技术】SpringBoot-Staters和自定义Starter
spring-boot-reference 如果不想阅读英文原文,你可以直接读本文的[TS]标注参考翻译内容.由于本人水平有限,如有理解错误,烦请指正,互相交流. Lire les documents ...
最新文章
- STM32中EXTI和NVIC的关系
- java sync_Java同步简介 - 加大装益达 - OSCHINA - 中文开源技术交流社区
- 在 asp.net core \ vs2015 update2 情况况下创建 asp.net core web application 的问题
- [工具]Mac下非常好用的快捷终端Dterm
- 不同的寻址方式的应用——将每行单词的前X个字母改为大写
- 工作日志之个人统计篇
- Linux 命令之 pgrep -- 用于检索(搜索进程/查找进程)当前正在运行的进程
- Macbook Pro笔记本双系统MacOS和Windows切换默认启动
- CI框架 CodeIgniter 伪静态 htaccess设置和Nginx伪静态方法
- pd.stats.ols.MovingOLS以及替代
- mysql学习day02
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_10-webpack研究-安装nodejs
- 关于cuda驱动版本以及cuda运行版本不匹配的问题
- 淘宝客商家如何加入内容商品库?
- 领导让我接私活,怎么办
- 怎样更改计算机文件图标,文件夹图标怎么改?电脑更换文件夹图标的方法
- 曾是谷歌程序员,抛下百万年薪创业,4年成就7亿用户,今身价百亿!
- C语言实现Dijkstra算法(求解两点之间最短路径问题)
- 四、node系列之购物车的业务逻辑
- 黑马程序员_石头迷阵小游戏