[英]Change default Port of Spring Boot application from frontend with thymeleaf
Is it possible to change the spring boot port from the frontend (by using thymeleaf) over a Endpoint on a @Controller?是否可以通过@Controller 上的端点从前端(通过使用百里香叶)更改 spring 引导端口?
And how can i programatically restart the application with the new configured port?以及如何使用新配置的端口以编程方式重新启动应用程序?
As stated here , you can set the server port like this:如此处所述,您可以像这样设置服务器端口:
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;
@Component
public class AppCustomContainer
implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(1234);
}
}
Instead of 1234, you can use a file to store the port (when changing it) and load it when starting您可以使用文件而不是 1234 来存储端口(更改端口时)并在启动时加载它
After that, you can restart the application as described here :之后,您可以按照此处所述重新启动应用程序:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.properties
: management.endpoint.restart.enabled = true
application.properties
中启用重启: management.endpoint.restart.enabled = true
RestartEndpoint
:RestartEndpoint
的实例:@Autowired
private RestartEndpoint restartEndpoint;
And run this in order to restart:并运行它以重新启动:
Thread restartThread = new Thread(restartEndpoint::restart);
restartThread.setDaemon(false);
restartThread.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.