SpringBoot工程web模式与非web模式和谐共处运行

一,概述

Spring Boot 是 Pivotal 团队在 Spring 的基础上提供的一套全新的开源框架,其目的是为了简化 Spring 应用的搭建和开发过程。Spring Boot 去除了大量的 XML 配置文件,简化了复杂的依赖管理。

Spring Boot 具有 Spring 一切优秀特性,Spring 能做的事,Spring Boot 都可以做,而且使用更加简单,功能更加丰富,性能更加稳定而健壮。随着近些年来微服务技术的流行,Spring Boot 也成了时下炙手可热的技术。

二,需求

假设我们现在有一个已经开发好的Spring Boot web项目,现在我们想要复用非web部分代码,并且希望能独立运行(不启用web服务的情况下)。

三,解决方案一:剥离非web部分代码

此方案是最容易想到的办法,具体就是新建springboot 非web项目,去除 spring-boot-starter-web 相关依赖和代码后调试。

1. 方案一弊端

需要维护2套代码,并需要手工保证2套非web部分功能代码的相对一致,代码维护麻烦

四,解决方案二:维护一套代码,指定以web或非web模式运行

1. 改造springboot启动类

修改前

   public static void main(String[] args)
    {
   
        SpringApplication.run(GitApplication.class, args);
    }

改造后:

   public static void main(String[] args)
    {
   
        boolean web = !ArrayUtils.contains(args, "--noWeb");
        log.info("############### with Web Configuration: {} #############", web);
        new SpringApplicationBuilder(GitApplication.class).web(web ? WebApplicationType.SERVLET : WebApplicationType.NONE).run(args);
    }

2. 移除web上下文注入或添加生效前提

针对controller类中,类似下文代码,移除HttpServletRequest等注入,当然也需要移除包含 RequestMappingHandlerMapping、HttpServletRequest、HttpSession、ServletContext 等web对象的注入。

  
    @Autowired
    HttpServletRequest request;
    
    @Operation(summary = "系统信息")
    @GetMapping(value = "/info")
    public Map<String, String> info()
        throws UnknownHostException
    {
   
        Map<String, String> infoMap = new HashMap<String, String>(2);
        infoMap.put("client_ip", IPUtils.getIpAddr(request));
        infoMap.put("server_ip", InetAddress.getLocalHost().getHostAddress());
        log.info("================== profile: {}", SpringContextUtils.getActiveProfile());
        staticCode.print();
        StaticCode2.print();
        return infoMap;
    }

改造为

  
    // @Autowired
    // HttpServletRequest request;
    
    @Operation(summary = "系统信息")
    @GetMapping(value = "/info")
    public Map<String, String> info(HttpServletRequest request)
        throws UnknownHostException
    {
   
        Map<String, String> infoMap = new HashMap<String, String>(2);
        infoMap.put("client_ip", IPUtils.getIpAddr(request));
        infoMap.put("server_ip", InetAddress.getLocalHost().getHostAddress());
        log.info("================== profile: {}", SpringContextUtils.getActiveProfile());
        staticCode.print();
        StaticCode2.print();
        return infoMap;
    }

或者在针对 controller 添加 @ConditionalOnWebApplication 注解

3. 排除其他web模式下 AutoConfiguration 的类

目前遇到的情况是非web模式下运行排除knife4j 自动注册类
添加DisableWebAutoConfig类

/**
 * 
 * 非web环境下禁用AutoConfiguration
 * 
 * @author 00fly
 * @version [版本号, 2023年4月4日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Configuration
@ConditionalOnNotWebApplication
@EnableAutoConfiguration(exclude = Knife4jAutoConfiguration.class)
public class DisableWebAutoConfig
{
   
}

4. 按照方案二改造成功的项目

https://gitee.com/00fly/effict-side/tree/master/springboot-git

https://gitee.com/00fly/effict-side/tree/master/springboot-hello

有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!

-over-

相关推荐

  1. SpringBoot工程web模式web模式和谐共处运行

    2024-02-01 19:22:02       38 阅读
  2. web开发模式

    2024-02-01 19:22:02       24 阅读
  3. Web自动化-PO模式

    2024-02-01 19:22:02       10 阅读
  4. 7.阻塞模式阻塞模式

    2024-02-01 19:22:02       12 阅读

最近更新

  1. hive 排序

    2024-02-01 19:22:02       0 阅读
  2. 小程序的制作费用很贵么

    2024-02-01 19:22:02       1 阅读
  3. c#实现23种常见的设计模式--动态更新

    2024-02-01 19:22:02       1 阅读
  4. 银河麒麟(V10SP1)-arm版交叉编译-qt-5.12.12源码

    2024-02-01 19:22:02       0 阅读

热门阅读

  1. 【数据结构1-4】图的基本应用

    2024-02-01 19:22:02       39 阅读
  2. react经验10:与jquery配合使用

    2024-02-01 19:22:02       31 阅读
  3. OpenAI Gym 中级教程----深入解析 Gym 代码和结构

    2024-02-01 19:22:02       29 阅读
  4. 一文详解docker compose

    2024-02-01 19:22:02       36 阅读
  5. centos 安装docker CE

    2024-02-01 19:22:02       41 阅读
  6. HIVE的数据类型-整型

    2024-02-01 19:22:02       39 阅读
  7. MySQL与PgSQL的优缺点对比

    2024-02-01 19:22:02       36 阅读
  8. SQL分页写法

    2024-02-01 19:22:02       34 阅读
  9. 征集各位的意见

    2024-02-01 19:22:02       29 阅读
  10. 【风水】-- 家居风水四大注意事项

    2024-02-01 19:22:02       31 阅读