栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自制Spring-boot Starter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自制Spring-boot Starter

  1. 新建Spring boot项目

    
      org.springframework.boot
      spring-boot-starter-parent
      2.7.0
       
    
    com.example
    web-filter-log-boot-starter
    
      org.springframework.boot
      spring-boot-starter-web
    
    
  2. 定义一个web过滤器,验证Spring boot starter是否生效

    // Filter
    public class LogFilter implements Filter {
    
        private Logger logger = LoggerFactory.getLogger(LogFilter.class);
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.err.println("logFilter init...");
            logger.info("logFilter init...");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
            logger.info("uri: {} is working.", httpServletRequest.getRequestURI());
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            logger.info("logFilter destroy...");
        }
    }
    
    // 配置Filter生效
    public class LogFilterRegistrationBean extends FilterRegistrationBean {
        public LogFilterRegistrationBean() {
            super();
            this.setFilter(new LogFilter());
            this.addUrlPatterns("/*");
            this.setName("LogFilter");
            this.setOrder(1);
        }
    }
    

    定义自动配置类

    // 自动配置类
    @Configuration
    @ConditionalOnClass({LogFilterRegistrationBean.class, LogFilter.class})
    public class LogFilterAutoConfiguration {
        @Bean
        @ConditionalOnMissingBean(LogFilterRegistrationBean.class)
        public LogFilterRegistrationBean logFilterRegistrationBean() {
            return new LogFilterRegistrationBean();
        }
    }
    
  3. 定义注解,使用注解将使该starter配置生效

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Import(EnableLogFilterImportSelector.class)
    public @interface EnableLogFilter {
    
    }
    
  4. 定义EnableLogFilterImportSelector用于从spring.factories加载starter自动配置类

    public class EnableLogFilterImportSelector implements DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
    
        private ClassLoader beanClassLoader;
        private Class annotationClass = EnableLogFilter.class;
    
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
    
            var attributes =
                    AnnotationAttributes.fromMap(importingClassMetadata
                            .getAnnotationAttributes(annotationClass.getName(), true));
            List factories = new ArrayList<>(
                    new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(this.annotationClass, this.beanClassLoader)));
            return factories.toArray(new String[factories.size()]);
        }
    
        @Override
        public void setEnvironment(Environment environment) {
    
        }
    
        @Override
        public void setBeanClassLoader(ClassLoader classLoader) {
            this.beanClassLoader = classLoader;
        }
    }
    
  5. 在resources/META-INF中添加spring.factories

    com.example.annotation.EnableLogFilter=com.example.config.LogFilterAutoConfiguration
    
  6. 使用starter

    
      com.example
      web-filter-log-boot-starter
    
    

    在启动类上添加EnableLogFilter注解,使starter自动配置生效

    @SpringBootApplication
    @EnableLogFilter
    public class DemoBootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoBootApplication.class, args);
        }
    
    }
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936597.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号