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

在其他项目引入就可以使用了

posted @   荣超  阅读(808)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示