java–Spring-Boot:如何添加tomcat连接器以绑定到控制器

java–Spring-Boot:如何添加tomcat连接器以绑定到控制器,第1张

概述在Spring-Boot文档中,有一节描述了如何为tomcat启用多个连接器(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/reference/htmlsingle/#howto-enable-multiple-connectors-in-tomcat).但有没有办法简单地将连接器添加到现有连接器(

在Spring-Boot文档中,有一节描述了如何为tomcat启用多个连接器(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/reference/htmlsingle/#howto-enable-multiple-connectors-in-tomcat).

但有没有办法简单地将连接器添加到现有连接器(Web和管理连接器)?并将它们绑定到一些mvc控制器
我想要做的是创建一些可以在不同端口上访问的Web服务.最佳答案您可以为每个服务使用子应用程序,并通过设置其server.port属性将每个子节点配置为使用单独的端口.您想要隔离的任何组件都应该包含在其中一个子组件中.您要共享的任何组件都应该包含在父级中.

这是这种方法的一个例子.有两个子应用程序,一个侦听端口8080,另一个侦听端口8081.每个应用程序包含一个控制器,分别映射到/ one和/ 2.

package com.example;import org.springframework.boot.autoconfigure.EnableautoConfiguration;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.bind.annotation.RequestMapPing;import org.springframework.web.bind.annotation.ResponseBody;public class Application {    public static voID main(String[] args) {        SpringApplicationBuilder parentBuilder                 = new SpringApplicationBuilder(ApplicationConfiguration.class);        parentBuilder.child(ServiceOneConfiguration.class)                .propertIEs("server.port:8080").run(args);        parentBuilder.child(ServiceTwoConfiguration.class)                .propertIEs("server.port:8081").run(args);          }    @Configuration    static class ApplicationConfiguration {        @Bean        public MySharedService sharedService() {            return new MySharedService();        }       }    @Configuration    @EnableautoConfiguration    static class ServiceOneConfiguration {        @Bean        public ControllerOne controller(MySharedService service) {            return new ControllerOne(service);        }    }    @Configuration    @EnableautoConfiguration    static class ServiceTwoConfiguration {        @Bean        public ControllerTwo controller(MySharedService service) {            return new ControllerTwo(service);        }    }    @RequestMapPing("/one")    static class ControllerOne {        private final MySharedService service;        public ControllerOne(MySharedService service) {            this.service = service;        }        @RequestMapPing        @ResponseBody        public String getMessage() {            return "ControllerOne says \"" + this.service.getMessage() + "\"";        }    }    @RequestMapPing("/two")    static class ControllerTwo {        private final MySharedService service;        public ControllerTwo(MySharedService service) {            this.service = service;        }        @RequestMapPing        @ResponseBody        public String getMessage() {            return "ControllerTwo says \"" + this.service.getMessage() + "\"";        }    }   static class MySharedService {        public String getMessage() {            return "Hello";        }    }}
总结

以上是内存溢出为你收集整理的java – Spring-Boot:如何添加tomcat连接器以绑定到控制器全部内容,希望文章能够帮你解决java – Spring-Boot:如何添加tomcat连接器以绑定到控制器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1265205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存