You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用类能否继承依赖Jar中的Spring Boot应用类?

Answers to Your Spring Boot Questions

Great questions! Let's break them down one by one with practical context and best practices:

1. Can a Spring Boot application class inherit another dependent Spring Boot application class?

  • Technically possible, but highly discouraged
    • Spring Boot application classes (marked with @SpringBootApplication) are just regular Java classes, so syntax-wise you can extend them. Here's a quick example:
      // Application class from your dependent JAR
      @SpringBootApplication
      public class DependentBootApp { ... }
      
      // Your application class
      @SpringBootApplication
      public class MyBootApp extends DependentBootApp {
          public static void main(String[] args) {
              SpringApplication.run(MyBootApp.class, args);
          }
      }
      
    • Why this is a bad idea:
      • @SpringBootApplication bundles @ComponentScan, @EnableAutoConfiguration, and @Configuration. Inheriting another app class means you might accidentally inherit its component scanning rules, auto-config settings, or bean definitions—things that aren't intended for your application. This often leads to duplicate beans, conflicting configurations, or unexpected component scanning ranges that are hard to debug.
      • Every Spring Boot app should have its own, clean entry point. Blurring this line by inheriting a dependency's app class makes maintenance messy and your app tightly coupled to the dependency's internal structure.

2. Can I inherit a Spring Boot application class from a dependent JAR to reuse Swagger configuration?

  • You can do it, but there are far better alternatives
    • If the dependent JAR's app class includes Swagger beans (like Docket definitions), extending it would technically pull those configs into your app. But again, you'll inherit all the baggage from the first question—unintended scans, conflicting beans, etc.
    • Recommended ways to reuse Swagger config:
      • Extract Swagger config to a dedicated @Configuration class in the dependent JAR:
        Move all Swagger-related code into a standalone config class, then import it into your app explicitly:
        // In the dependent JAR
        @Configuration
        @EnableSwagger2
        public class SharedSwaggerConfig {
            @Bean
            public Docket api() {
                return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.shared"))
                    .paths(PathSelectors.any())
                    .build();
            }
        }
        
        // In your app's main class
        @SpringBootApplication
        @Import(SharedSwaggerConfig.class)
        public class MyBootApp { ... }
        
      • Use Spring Boot auto-configuration:
        If you control the dependent JAR, register the Swagger config in META-INF/spring.factories:
        org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.SharedSwaggerConfig
        
        Now, adding the JAR as a dependency will automatically apply the Swagger config without any extra code in your app.
      • If you absolutely must inherit:
        If you have no other option, adjust your app's scanning rules to avoid conflicts:
        @SpringBootApplication(scanBasePackages = "com.myapp")
        public class MyBootApp extends DependentBootApp { ... }
        
        This restricts your app's component scanning to your own packages, preventing unintended beans from the dependency being picked up.

内容的提问来源于stack exchange,提问作者Subramanian Palaniappan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:52:52