Spring-boot: Fails to start when using @SpringBootApplication with SpringApplicationBuilder.sources

Created on 26 Feb 2018  路  6Comments  路  Source: spring-projects/spring-boot


use @SpringBootApplication with SpringApplicationBuilder锛宮y app starts failed

// main method
new SpringApplicationBuilder()
                .sources(ParentConfig.class)
                .child(Child1Config.class)
                .sibling(Child2Config.class)
                .banner(new AppBanner())
                .logStartupInfo(false)
                .run(args);
// ParentConfig
@SpringBootApplication
@PropertySource("classpath:/parent.properties")
public class ParentConfig {
}
// Child1Config 
@SpringBootApplication
@PropertySource("classpath:/child1.properties")
public class Child1Config {

}
// Child2Config 
@SpringBootApplication
@PropertySource("classpath:/child2.properties")
public class Child2Config {

}

the error log as follow:

Description:

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.

2018-02-26 14:30:41.239  INFO 33664 --- [           main] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@109f5dd8: startup date [Mon Feb 26 14:30:39 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@a4102b8
2018-02-26 14:30:41.242  INFO 33664 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

the version of springboot is "2.0.0.RC2" or "2.0.0.BUILD-SNAPSHOT"

but if i change the version to "1.5.10.RELEASE", it work fine.
Another solutation is change the @SpringBootApplication to @Configuration in ParentConfig.java.

I don't know why, can anyone tell me? Thank you very much!!!!

stackoverflow invalid

Most helpful comment

I have also faced the same issue, to overcome this issue you can kill the PID using following commands.

C:\Users\Admin> netstat -ano | findstr :8080

It will show as below:

  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       6956
  TCP    [::]:8080              [::]:0                 LISTENING       6956

Then run the below command to kill the PID

C:\Users\Admin>taskkill /PID 6956 /F
SUCCESS: The process with PID 6956 has been terminated.

Now you can re run the Spring application smoothly. Thank you!!

All 6 comments

You have started multiple SpringApplications with a web environment. Two or more of them are configured to use the same port so the second fails with a port clash. Also, your use of PropertySource is not recommended in a Spring Boot app as the configuration it contains may be available too late.

In the future please ask this sort of question on Gitter or Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

@wilkinsona Thank you for answering me!
I'll do it as you say.

I just want to know why the above code can run normally in version 1.x, and there will be a port conflict error in version 2.x. What's the reason for the change in version 2.

Thanks again.

There's been no change in Spring Boot 2 in this area. The same arrangement will fail with 1.5:

2018-02-26 09:19:18.238  INFO 61917 --- [           main] c.e.demo.parent.Gh12219Application       : Starting Gh12219Application on aw-rmbp.local with PID 61917 (/Users/awilkinson/dev/workspaces/spring/spring-boot/master/gh-12219/target/classes started by awilkinson in /Users/awilkinson/dev/workspaces/spring/spring-boot/master/gh-12219)
2018-02-26 09:19:18.241  INFO 61917 --- [           main] c.e.demo.parent.Gh12219Application       : No active profile set, falling back to default profiles: default
2018-02-26 09:19:18.284  INFO 61917 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7f416310: startup date [Mon Feb 26 09:19:18 GMT 2018]; root of context hierarchy
2018-02-26 09:19:19.018  INFO 61917 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-02-26 09:19:19.030  INFO 61917 --- [           main] c.e.demo.parent.Gh12219Application       : Started Gh12219Application in 1.328 seconds (JVM running for 1.642)

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

2018-02-26 09:19:19.057  INFO 61917 --- [           main] c.e.demo.parent.Gh12219Application       : No active profile set, falling back to default profiles: default
2018-02-26 09:19:19.058  INFO 61917 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ea6e30c: startup date [Mon Feb 26 09:19:19 GMT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7f416310
2018-02-26 09:19:19.455  INFO 61917 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-02-26 09:19:19.468  INFO 61917 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-02-26 09:19:19.469  INFO 61917 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-02-26 09:19:19.548  INFO 61917 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-02-26 09:19:19.548  INFO 61917 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 490 ms
2018-02-26 09:19:19.618  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-02-26 09:19:19.621  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-02-26 09:19:19.621  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-02-26 09:19:19.621  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-02-26 09:19:19.622  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-02-26 09:19:19.760  INFO 61917 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ea6e30c: startup date [Mon Feb 26 09:19:19 GMT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7f416310
2018-02-26 09:19:19.819  INFO 61917 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-02-26 09:19:19.820  INFO 61917 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-02-26 09:19:19.844  INFO 61917 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-26 09:19:19.844  INFO 61917 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-26 09:19:19.873  INFO 61917 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-26 09:19:19.959  INFO 61917 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-02-26 09:19:20.017  INFO 61917 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-02-26 09:19:20.020  INFO 61917 --- [           main] c.e.demo.parent.Gh12219Application       : Started Gh12219Application in 0.99 seconds (JVM running for 2.632)

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

2018-02-26 09:19:20.045  INFO 61917 --- [           main] c.e.demo.parent.Gh12219Application       : No active profile set, falling back to default profiles: default
2018-02-26 09:19:20.046  INFO 61917 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7e8e8651: startup date [Mon Feb 26 09:19:20 GMT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7f416310
2018-02-26 09:19:20.185  INFO 61917 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-02-26 09:19:20.186  INFO 61917 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-02-26 09:19:20.186  INFO 61917 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-02-26 09:19:20.191  INFO 61917 --- [ost-startStop-1] o.a.c.c.C.[Tomcat-1].[localhost].[/]     : Initializing Spring embedded WebApplicationContext
2018-02-26 09:19:20.192  INFO 61917 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 146 ms
2018-02-26 09:19:20.230  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-02-26 09:19:20.230  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-02-26 09:19:20.230  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-02-26 09:19:20.230  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-02-26 09:19:20.230  INFO 61917 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-02-26 09:19:20.279  INFO 61917 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7e8e8651: startup date [Mon Feb 26 09:19:20 GMT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7f416310
2018-02-26 09:19:20.288  INFO 61917 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-02-26 09:19:20.288  INFO 61917 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-02-26 09:19:20.300  INFO 61917 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-26 09:19:20.300  INFO 61917 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-26 09:19:20.314  INFO 61917 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-26 09:19:20.371  INFO 61917 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-02-26 09:19:20.383 ERROR 61917 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:250) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:193) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at com.example.demo.parent.Gh12219Application.main(Gh12219Application.java:16) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1021) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    ... 12 common frames omitted
Caused by: java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_151]
    at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_151]
    at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_151]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_151]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_151]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1150) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:591) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1018) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    ... 13 common frames omitted

2018-02-26 09:19:20.384  INFO 61917 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-02-26 09:19:20.393  INFO 61917 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-02-26 09:19:20.396 ERROR 61917 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.

2018-02-26 09:19:20.396  INFO 61917 --- [           main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7e8e8651: startup date [Mon Feb 26 09:19:20 GMT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7f416310
2018-02-26 09:19:20.397  INFO 61917 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

If you're seeing different behaviour then there is a difference in your configuration that you haven't described. If you need further guidance, please ask a question on Stack Overflow and provide a minimal, complete, and verifiable example with it.

@wilkinsona thank you锛宨 will do it

I have also faced the same issue, to overcome this issue you can kill the PID using following commands.

C:\Users\Admin> netstat -ano | findstr :8080

It will show as below:

  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       6956
  TCP    [::]:8080              [::]:0                 LISTENING       6956

Then run the below command to kill the PID

C:\Users\Admin>taskkill /PID 6956 /F
SUCCESS: The process with PID 6956 has been terminated.

Now you can re run the Spring application smoothly. Thank you!!

Run the command with the port used : example : lsof -i : 8080(Port number)

Now kill the process. command -: kill -9 (example : kill -9 8080).

Was this page helpful?
0 / 5 - 0 ratings