【发布时间】:2015-11-25 11:47:08
【问题描述】:
我正在尝试构建一个包含两个 Sprin Boot 应用程序的测试设置。 这两个应用程序都有一个单独的类。
这两个应用程序看起来像这样:(但它们是不同的、独立的类)
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class MySpringBootApplet {
@RequestMapping("/")
public String home() {
System.out.println("home() called ..");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("waited ..");
return "<!DOCTYPE html><html><body><h1>Test</h1><p>Hello world!</p></body></html>";
}
两者都以
开头SpringApplication app = new SpringApplication(MySpringBootApplet.class);
app.run();
当第二个应用启动时,我得到了错误:
org.springframework.jmx.export.UnableToRegisterMBeanException: 无法 注册 MBean [org.springframework.boot.actuate.endpoint.jmx.DataEndpointMBean@6a48a7f3] 带有键“requestMappingEndpoint”;嵌套异常是 javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Endpoint,name=requestMappingEndpoint
我可以想象这是因为两个应用程序都尝试使用相同的接口进行注册。但是我该如何区分呢?
感谢您的帮助
【问题讨论】:
-
正确,它们发生冲突。而且我认为您不能像这样在同一个 JVM 进程中运行 2 个 应用程序。你想要的原因是什么?也许有更好的解决方案。
-
我基本上需要通过 RESTful 接口进行通信的应用程序来测试一些东西。
-
您可以有 2 个使用不同端口的独立项目(因此它们可以在 1 台机器上运行),或者您可以摆脱第二个应用程序,因为 1 个应用程序可以有超过 1 个休息控制器,您可以 @987654325喜欢的话@给自己。
标签: java spring spring-boot