当前位置: 首页 > 知识库问答 >
问题:

多上下文spring boot应用程序:如何为每个子上下文定义标准spring boot属性

乐正锦
2023-03-14

添加多个tomcat连接器并将其绑定到每个单独的控制器的能力有一个很好的问题。

安迪·威尔金森好答案的精髓在这里:

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);      
}

我想进一步问另一个问题:

有没有一种方法可以让每个子应用程序上下文在支持所有标准spring boot功能的情况下读取一些特定于子应用程序的属性,比如配置文件后缀文件名等。

有三个单独的属性文件:

  • 应用程序。属性

并且能够为每个上下文设置一个文件。

但对我来说重要的是,他们必须支持所有的spring boot magic。例如,如果我激活了新的配置文件,比如传递命令行参数--spring。个人资料。active=dev,则不仅应自动加载这三个文件(每个上下文一个),还应自动加载另一组文件(如果存在):

  • application-dev.properties

当然,这些文件应该按照定义的顺序在标准Spring引导支持的路径上搜索。

如果盒子里可能有一些编码,有可能吗?

只有一个应用程序。属性文件(支持配置文件等),但以某种方式将前缀属性映射到子上下文的非固定属性。

例子:

child1.server.port=8081
child1.foo=bar
child2.server.port=8082
child2.foo=baz

第一个子上下文应该看到这些属性,就像它们是:

server.port=8081
foo=bar

第二个子上下文应该看到这些属性,就像它们是:

server.port=8082
foo=baz

因此,标准spring boot的自动配置将起作用,并正确设置tomcat的端口等。

我不是在寻找具体的方法(但如果你问我,我倾向于第二种方法),但任何开箱即用的方法或用最低条件的方法可以实现的方法都足够了。

共有1个答案

赵景曜
2023-03-14

您可以使用spring实现您的第一个建议。配置。姓名

public static void main(String[] args) {
    SpringApplicationBuilder parentBuilder =
            new SpringApplicationBuilder(ParentApplication.class)
                    .web(WebApplicationType.NONE);
    parentBuilder.run(args);
    parentBuilder.child(ServiceOneConfiguration.class)
            .properties("spring.config.name=child1").run(args);
    parentBuilder.child(ServiceTwoConfiguration.class)
            .properties("spring.config.name=child2").run(args);
}

然后,您可以使用Chil1{-profile}. PropertiesChil2{-profile}. Properties分别配置服务1和服务2。

例如,在child1.properties中的server.port=8081child2.properties中的server.port=8082中,当启动在两个子服务中使用自动配置并且依赖于Spring-boot-starter-web的应用时,您应该会看到类似于以下的输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-22 13:38:09.690  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Starting ParentApplication on …
2019-01-22 13:38:09.692  INFO 80968 --- [           main] com.example.parent.ParentApplication     : No active profile set, falling back to default profiles: default
2019-01-22 13:38:09.842  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Started ParentApplication in 0.371 seconds (JVM running for 0.644)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-22 13:38:10.046  INFO 80968 --- [           main] com.example.parent.ParentApplication     : No active profile set, falling back to default profiles: default
2019-01-22 13:38:10.584  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-01-22 13:38:10.604  INFO 80968 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-22 13:38:10.605  INFO 80968 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-22 13:38:10.613  INFO 80968 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/awilkinson/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-01-22 13:38:10.668  INFO 80968 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-22 13:38:10.668  INFO 80968 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 612 ms
2019-01-22 13:38:10.829  INFO 80968 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-22 13:38:10.981  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-01-22 13:38:10.981  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Started ParentApplication in 0.955 seconds (JVM running for 1.784)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-22 13:38:11.003  INFO 80968 --- [           main] com.example.parent.ParentApplication     : No active profile set, falling back to default profiles: default
2019-01-22 13:38:11.093  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2019-01-22 13:38:11.095  INFO 80968 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-22 13:38:11.096  INFO 80968 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-22 13:38:11.100  INFO 80968 --- [           main] o.a.c.c.C.[Tomcat-1].[localhost].[/]     : Initializing Spring embedded WebApplicationContext
2019-01-22 13:38:11.101  INFO 80968 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 97 ms
2019-01-22 13:38:11.135  INFO 80968 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-22 13:38:11.164  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2019-01-22 13:38:11.165  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Started ParentApplication in 0.183 seconds (JVM running for 1.967)
 类似资料:
  • 我已经使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包生成了一个Spring Boot web应用程序。 使用的技术: Spring启动2.0.0。M6,Java8, Maven 这是我的安全配置 在我的 但当我在http://localhost:1234/iberiaWebUtils,而不是去http://localhost:1234/ibe

  • 我多年来一直在使用Spring MVC,我试图理解与Spring Boot的一些关键区别。 你能帮我确认一下吗?或者让我明白我在这里遗漏了什么?

  • 我通过将上下文路径设置为/myservice来运行我的springboot应用程序。这将导致附加在URL处公开的所有执行器endpoint-http://localhost:8080/myservice/actuator/,而我只想要http://localhost:8080/actuator/.有没有办法告诉springboot忽略将上下文路径附加到执行器endpoint(通过Dispatche

  • 问题内容: 到目前为止,我以前一直认为Web应用程序只能具有我们在 我这样想对吗? 我可以在一个Web应用程序中拥有多个调度程序Servlet吗?如果是,如何? 在什么情况下我们可能需要这样做? 整个Web应用程序中只能有一个应用程序上下文吗? 我们如何定义多个应用程序上下文? 非Spring应用程序中可以存在吗? 问题答案: 一个Web应用程序中可以有多个调度程序servlet吗? Web应用程

  • 当前目录的Spring Boot多上下文问题 在Spring Boot文档应用程序属性文件中 null 在和模块的PluginContext中不可见,只能在中可见,因为它是root,但当前目录在哪里?当前目录是什么意思? 是否可以从自己的模块将包含到PluginContext中,而不需要其他配置(通过或外部配置)?

  • 寻找在Springboot应用程序中配置多个配置文件特定属性文件的最佳方法。下面是一个例子: -资源  · --application.properties  · · · · · --德夫             --application-dev.properties             --ldap-dev.properties             --Quartz-Dev.Prope