简体   繁体   English

spring 引导应用程序 class 是否可以扩展另一个依赖的 spring 引导应用程序 ZA2F2ED4F8EBC2CBB14C21A29ZDC4

[英]Can a spring boot application class extend another dependent spring boot application class

I have one spring boot project (jar) as dependent jar in another spring boot application, in the current project/application i dont want to have swagger sort of codes to be written or configured again, so is it possible to extend the spring boot application class from the dependent jar which has those configurations I have one spring boot project (jar) as dependent jar in another spring boot application, in the current project/application i dont want to have swagger sort of codes to be written or configured again, so is it possible to extend the spring boot application class 来自具有这些配置的从属 jar

There is a way to do it but I'd strongly recommend you do not do it.有一种方法可以做到,但我强烈建议你要这样做。 Spring boot applications are packaged differently to standard jars.. they're executable jars with other bits and bobs going on internally. Spring 引导应用程序的打包方式与标准 jars 不同。它们是可执行的 jars,内部还有其他一些东西。 If you plan on running the spring boot jar anywhere other than locally (eg within a container) it is likely to fail.如果您计划在本地以外的任何地方(例如在容器内)运行 spring 引导 jar,它可能会失败。

I'd recommend extracting out the common functionality to a shared module instead and add a dependency on the shared module to each parent module.我建议将通用功能提取到共享模块中,并将对共享模块的依赖项添加到每个父模块。 This works well if you're using Gradle/maven.如果您使用 Gradle/maven,这很有效。

Yes for a concept it can be done the following way, just make sure the the version of dependency match to that of one extending it是的,对于一个概念,它可以通过以下方式完成,只需确保依赖项的版本与扩展它的版本匹配

Project 1项目一

  1. Creating a Simple Hello World Spring Web project " projectone " using Spring starter Project使用 Spring 启动项目创建一个简单的 Hello World Spring Web 项目“ projectone
    <groupId>com.harshit</groupId>
    <artifactId>projectone</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>projectone</name>
  1. @RestController with following API @RestController 与以下 API
    @GetMapping("/one")
    public String helloworld()
    {
        return "Project One Works";
        
    }
    
    @GetMapping("/alone")
    public String checkmethod()
    {
        return "alone method also made its way into the second project";
        
    }
  1. Build / run /install.构建/运行/安装。 the project on server.port=8081 server.port=8081 上的项目

    mvn clean install

Project 2项目 2

  1. Creating another Simple Spring Web project " projecttwo " using Spring starter Project使用 Spring 启动项目创建另一个简单 Spring Web 项目“ projecttwo
    <groupId>com.demo</groupId>
    <artifactId>projecttwo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>projecttwo</name>
  1. Add the previously created " projectione " as dependency in this project in the pom.xm of this project在本项目的 pom.xm 中添加之前创建的“ projectione ”作为本项目的依赖
        <dependency>
            <groupId>com.harshit</groupId>
            <artifactId>projectone</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
  1. Create a @RestController with the following api extending the Controller class of the " projectione "使用以下 api 创建一个 @RestController 扩展“ projectione ”的 Controller class
import com.harshit.controller.Controller;
@RestController
public class anothercontroller  extends Controller{
    
    
    @GetMapping("/two")
    public String hellomultiverse()
    {
        return helloworld();
        
    }
  @GetMapping("/three")
    public String multiverse()
    {
        return "threeworks";
        
    }
    
    @Override
    public String helloworld()
    {
        return "done ";
        
    }
    
    
  1. Build / run /install.构建/运行/安装。 the project on server.port=9999 server.port=9999 上的项目
mvn clean install

Testing the Extensions测试扩展

Project项目 API API Reachable可达
"projectone" method extended in "projecttwo"在“projecttwo”中扩展的“projectone”方法 Localhost:9999/alone本地主机:9999/单独 YES是的
"projectone" method extended + overridden in "projecttwo" “projectone”方法在“projecttwo”中扩展+覆盖 Localhost:9999/two本地主机:9999/二 YES是的
"projecttwo" method “项目二”方法 Locahost:9999/three本地主机:9999/三 YES是的
"projectone" method extended + overridden in "projecttwo" “projectone”方法在“projecttwo”中扩展+覆盖 Localhost:9999/one本地主机:9999/一个 YES (Overridden output)是(覆盖输出)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot 应用程序中的类变量 - Class Variables in Spring boot application Spring Boot,测试主应用类 - Spring Boot, test main application class Maven 验证 Spring Boot 应用程序类的 ClassNotFoundException - Maven verify ClassNotFoundException for class of Spring Boot application 在Spring Boot应用程序中共享类的实例 - Sharing an instance of a class across a spring boot application Spring Boot应用程序。 入门类无法找到.xml文件 - Spring boot application. the starter class can not find .xml file Spring 引导无法使用依赖的应用程序。属性 - Spring Boot not working with dependent application.properties 将spring boot应用程序添加为另一个Spring启动应用程序的依赖项 - Add spring boot application as dependency to another spring boot application 是否可以将一个 Spring Boot 应用程序嵌套在另一个 Spring Boot 应用程序中? - Is it possible to nest a Spring Boot application inside another Spring Boot application? Spring Boot Application如何在没有@Configuration类的情况下创建bean - How does a Spring Boot Application create beans without @Configuration class Spring Boot应用程序无法实例化一个类,除非使用Autowired对其进行了注释 - Spring Boot application cannot instantiate a class unless it is annotated with Autowired
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM