简体   繁体   English

使用 thymeleaf 从前端更改 Spring 的默认端口

[英]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 :之后,您可以按照此处所述重新启动应用程序:

  • Add actuator and spring-cloud:添加执行器和弹簧云:
<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>
  • Enable restarting in the application.properties : management.endpoint.restart.enabled = trueapplication.properties中启用重启: management.endpoint.restart.enabled = true
  • Add an the instance of 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.

相关问题 将js从文件系统加载到spring boot jar应用程序中(使用thymeleaf) - load js from filesystem into spring boot jar application (with thymeleaf) 默认 Spring Boot Thymeleaf 配置位置 - Default Spring Boot Thymeleaf configuration location 如何使用Spring Boot应用程序更改嵌入式tomcat连接器端口 - How to change embedded tomcat connector port using spring boot application 在不更改代码的情况下更改 Spring Boot 应用程序的端口 - Change the port of a Spring boot application without changing the code 如何通过Spring Boot应用程序和Spock测试在运行时更改服务器端口 - How to change server port in runtime with a spring boot application and spock testing 无法使用 thymeleaf 运行 spring boot 应用程序 - Unable to run spring boot application with thymeleaf Spring 引导 CRUD 应用程序与 Thymeleaf - BindingResult 结果 - Spring Boot CRUD Application with Thymeleaf - BindingResult result Spring Boot和Thymeleaf应用程序:jquery正在加载但未执行 - Spring boot and Thymeleaf application: jquery is loading but not executing 如果未登录,则在Spring Boot / Thymeleaf应用程序中隐藏按钮 - Hide a button in Spring Boot/Thymeleaf application if not logged in 在IntelliJ中启动Spring Boot + thymeleaf应用程序的Faild - Faild to Start Spring Boot +thymeleaf application in IntelliJ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM