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:
@SpringBootApplicationbundles@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.
- Spring Boot application classes (marked with
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
Docketdefinitions), 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
@Configurationclass 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 inMETA-INF/spring.factories:
Now, adding the JAR as a dependency will automatically apply the Swagger config without any extra code in your app.org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.SharedSwaggerConfig - If you absolutely must inherit:
If you have no other option, adjust your app's scanning rules to avoid conflicts:
This restricts your app's component scanning to your own packages, preventing unintended beans from the dependency being picked up.@SpringBootApplication(scanBasePackages = "com.myapp") public class MyBootApp extends DependentBootApp { ... }
- Extract Swagger config to a dedicated
- If the dependent JAR's app class includes Swagger beans (like
内容的提问来源于stack exchange,提问作者Subramanian Palaniappan
相关产品推荐
相关产品推荐

