[英]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是的,对于一个概念,它可以通过以下方式完成,只需确保依赖项的版本与扩展它的版本匹配
<groupId>com.harshit</groupId>
<artifactId>projectone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>projectone</name>
@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";
}
Build / run /install.构建/运行/安装。 the project on server.port=8081
server.port=8081 上的项目
mvn clean install
<groupId>com.demo</groupId>
<artifactId>projecttwo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>projecttwo</name>
<dependency>
<groupId>com.harshit</groupId>
<artifactId>projectone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
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 ";
}
mvn clean install
Project![]() |
API ![]() |
Reachable![]() |
---|---|---|
"projectone" method extended in "projecttwo"![]() |
Localhost:9999/alone![]() |
YES![]() |
"projectone" method extended + overridden in "projecttwo" ![]() |
Localhost:9999/two![]() |
YES![]() |
"projecttwo" method ![]() |
Locahost:9999/three![]() |
YES![]() |
"projectone" method extended + overridden in "projecttwo" ![]() |
Localhost:9999/one![]() |
YES (Overridden output)![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.