zoukankan      html  css  js  c++  java
  • springboot 简单自定义starter

    使用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

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

  • 相关阅读:
    计算机网络复习(二) 应用层
    JavaScript实战笔记(二) 数组去重
    计算机网络复习(一) 基本介绍
    计算机网络复习
    Git学习笔记(一) 常用命令
    Git学习笔记
    Python实战笔记(三) 多线程
    Python实战笔记(二) 网络编程
    Python学习笔记
    XBox360自制系统的更新(Update)
  • 原文地址:https://www.cnblogs.com/rchao/p/10917256.html
Copyright © 2011-2022 走看看