企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 3.5 Spring Boot集成 支持SpringBoot2和SpringBoot3 ### 3.5.1 Spring Boot 3 ~~~xml <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-springboot-starter-jdk17</artifactId> <version>3.16.2.RELEASE</version> </dependency> ~~~ Java代码 ,使用@EnableBeetl ```java @SpringBootApplication @EnableBeetl public class TestSpringBootApplication { public static void main(String[] args) { SpringApplication.run(TestSpringBootApplication.class, args); } } ``` starter 自动处理以btl结尾的视图,模板根目录是Spring Boot默认的templates目录。如下配置可以修改beetl部分属性 * beetl-beetlsql.dev,默认为true,即自动检查模板变化 * beetl.enabled 默认为true,集成beetl。 * beetl.suffix 默认为btl,表示只处理视图后缀为btl的模板,比如controller里代码是“return /common/index.btl”,则能被Beetl处理,你写成"return /common/index",或者"/common/index.html",都会出现404错误。 Starter可以实现BeetlTemplateCustomize来定制Beetl ~~~java @Configuration public MyConfig{ @Bean public BeetlTemplateCustomize beetlTemplateCustomize(){ return new BeetlTemplateCustomize(){ public void customize(GroupTemplate groupTemplate){ } }; } } ~~~ ### 3.5.2 Spring Boot 2 ~~~xml <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-spring-boot-starter-classic</artifactId> <version>3.14.1.RELEASE</version> </dependency> ~~~ Java代码 ,使用@EnableBeetl ```java @SpringBootApplication @EnableBeetl public class TestSpringBootApplication { public static void main(String[] args) { SpringApplication.run(TestSpringBootApplication.class, args); } } ``` 关于Beetl配置同Spring Boot3 ### 3.5.3 兼容 Spring Boot 2 (不推荐) 在3.14版本之前,使用的是`beetl-framework-starter` ,也适用于Spring Boot 2 ~~~xml <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-framework-starter</artifactId> <version>1.2.31.Beetl.RELEASE</version> </dependency> ~~~ 这种方式不需是Spring Starter方式,自动集成Beetl,不需要@EnableBeetl 注解 ### 3.5.4 自己集成 无论上面那种方式,本质上是构造BeetlGroupUtilConfiguration,和创建 BeetlSpringViewResolver实例,可以自己定义集成方式,参考代码如下 ```java @Configuration public class BeetlConf { @Value("${beetl.templatesPath}") String templatesPath;//模板根目录 ,比如 "templates" @Bean(name = "beetlConfig") public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() { BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration(); //获取Spring Boot 的ClassLoader ClassLoader loader = Thread.currentThread().getContextClassLoader(); if(loader==null){ loader = BeetlConf.class.getClassLoader(); } beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要 ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, templatesPath); beetlGroupUtilConfiguration.setResourceLoader(cploder); beetlGroupUtilConfiguration.init(); //如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader); return beetlGroupUtilConfiguration; } @Bean(name = "beetlViewResolver") public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) { BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver(); beetlSpringViewResolver.setContentType("text/html;charset=UTF-8"); beetlSpringViewResolver.setOrder(0); beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration); return beetlSpringViewResolver; } } ```