springapplicationbuilder parent webapp和没有webApp的孩子没有工作
我有一个带有父级应用程序上下文的Spring Boot应用程序和带有一些旧版代码的子女应用程序上下文。我想在父级应用程序上下文中拥有一个WebApp,并使子上下文是一个非WebApp。
I have my config and properties in application.yml, that gets loaded in the main class
@ComponentScan(...)
@PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication(
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.profiles("Prod")
.web(WebApplicationType.SERVLET)
.listeners(new MainAppListener()) // logging for some of the events
.child(LegacyServiceConfig.class)
.web(WebApplicationType.NONE)
.listeners(new LegacyAppListener()) // logging for some of the events
.run(args);
}
}
What I've tried so far:
- I don't have any web application type config in my application.yml (
spring.main.web -application-type
)。我希望我通过代码指定的配置在上面的片段中可以使用,但事实并非如此。两个应用程序上下文都没有以WebApp开头,有效地忽略了上面段中的ConfigWeb(WebApplicationType.servlet)
。 - 我尝试在我的 application.yaml。但这似乎将此配置应用于两个父母 和儿童应用程序上下文。他们俩都试图开始 WebApps,这不是我想要的。
我该如何工作?有指针吗?
I have a spring boot app with a parent application context and a child application context with some legacy code. I would like to have a webapp in the parent application context, and have the child context be a non webapp.
I have my config and properties in application.yml, that gets loaded in the main class
@ComponentScan(...)
@PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication(
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.profiles("Prod")
.web(WebApplicationType.SERVLET)
.listeners(new MainAppListener()) // logging for some of the events
.child(LegacyServiceConfig.class)
.web(WebApplicationType.NONE)
.listeners(new LegacyAppListener()) // logging for some of the events
.run(args);
}
}
What I've tried so far:
- I don't have any web application type config in my application.yml (
spring.main.web-application-type
). I hoped that the configuration I've specified thru code, in the above snippet would have worked, but it doesn't. Neither of the application contexts start as a webapp, effectively ignoring the configweb(WebApplicationType.SERVLET)
in the above snippet. - I tried putting
spring.main.web-application-type=servlet
in my
application.yaml. But that seems to apply this config to both parent
and child application context. And both of them try to start as
webapps, which is not what I want.
How do I get this to work? Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我在Spring-Boot中的知识,仅由Main开始,Maven提供了Maven,因为我从事Maven Project。项目中的所有属性,例如JDBC连接,端口更改您可以在.properties(扩展)类型文件中进行。而且该文件也可以采用.yml的格式。这些文件与主类完全不同。
如果您对主进行任何更改或添加任何额外的注释,则会出现错误。
因此,将每个属性保留在.properties或.yml文件中,以及所有配置组件扫描中的pom.xml文件您也可以有两个.properties文件,
但是主类应该很简单:
希望这会有所帮助。
As per my knowledge in spring-boot start from main only and the main class is provided by Maven, because I had worked on Maven project. All the properties in the project like JDBC connection, port change you can do it in .properties(extension) type file. And this file can also be in format of .yml also. These file is totally different from main class.
If you make any changes in main or add any extra annotation in main then it will throw an error.
So keep every properties in .properties or .yml file and all configuration component scan in pom.xml file you can have two .properties file also
But main class should be simple like this:
Hope this will help.