初学SpringBoot之一

Posted 神跳跳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初学SpringBoot之一相关的知识,希望对你有一定的参考价值。

学习的时候参考的这篇教程,http://blog.csdn.net/lxhjh/article/details/51751847

---------------------------------------------------------------------------------------------------------------------------------------------

主要发现了几个问题

1.SpringBoot可以直接以run as java application形式启动,确实也省去了配置XML、给tomcat中添加工程的步骤,启动tomcat的过程这里也成了启动java application。

2.SpringBoot在关闭eclipse时会自动释放tomcat 8080端口,不需要再手动关闭tomcat释放8080。

3.发现main函数所在的类的注解为SpringBootApplication,而main函数中就一句话SpringApplication.run(SpringBootSampleHelloworldApplication.class, args),且需要    引入包为org.springframework.boot.SpringApplication及org.springframework.boot.autoconfigure.SpringBootApplication,猜测这里是直接将SpringBootApplication作    为一个组件放到SpringApplication中去运行,且因为SpringBootApplication是在autoconfigure包下,猜测这就是所谓的SpringBoot能够自动配置。

4.发现SpringBoot运行后,再次修改controller中的逻辑貌似没什么用,比如修改对应路径下的输出内容,修改前后输出居然一样。。感觉真是像见了鬼。。,莫非是其    它地方出了问题,但是没有发现。。

5.最后一点,发现controller所在的包貌似不用和SpringBoot所在的包一致,。。

以上大多为猜测,具体SpringBoot的内容期待后续的学习。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这里记录一个遇到的端口问题:

其实就是8080端口占用问题,

问题场景:一开始由于想要测试SpringBoot和RestController的相对位置问题,错写了两个一模一样的SpringBoot,先前的已启动,后来启动的是另一个,提示端口占用,文章最后为consolo提示内容。

解决办法:简单的可以直接到任务管理器关掉javaw.exe。这里发现管理员运行模式下的cmd输入netstat -abn可以查看所有连接的程序,非常神奇(果然是太菜。。)。

consolo的输出

(下图应为spring的字母图形,可能是编码原因,显示有问题,暂且不管,先往后走)
. ____ _ __ _ _
/\\\\ / ___\'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | \'_ | \'_| | \'_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
\' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE)

2017-06-24 18:37:53.878 INFO 3712 --- [ main] .e.SpringBootSampleHelloworldApplication : Starting SpringBootSampleHelloworldApplication on OQBNBM1JE2S28LI with PID 3712 (Z:\\ChromeDownloads\\spring-boot-sample-helloworld\\spring-boot-sample-helloworld\\target\\classes started by Administrator in Z:\\ChromeDownloads\\spring-boot-sample-helloworld\\spring-boot-sample-helloworld)
2017-06-24 18:37:53.885 INFO 3712 --- [ main] .e.SpringBootSampleHelloworldApplication : No active profile set, falling back to default profiles: default
2017-06-24 18:37:54.215 INFO 3712 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f51b3e0: startup date [Sat Jun 24 18:37:54 CST 2017]; root of context hierarchy
2017-06-24 18:37:59.653 INFO 3712 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-24 18:37:59.692 INFO 3712 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-06-24 18:37:59.695 INFO 3712 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-06-24 18:37:59.964 INFO 3712 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-24 18:37:59.965 INFO 3712 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5765 ms
2017-06-24 18:38:00.296 INFO 3712 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: \'dispatcherServlet\' to [/]
2017-06-24 18:38:00.307 INFO 3712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: \'characterEncodingFilter\' to: [/*]
2017-06-24 18:38:00.308 INFO 3712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: \'hiddenHttpMethodFilter\' to: [/*]
2017-06-24 18:38:00.309 INFO 3712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: \'httpPutFormContentFilter\' to: [/*]
2017-06-24 18:38:00.309 INFO 3712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: \'requestContextFilter\' to: [/*]
2017-06-24 18:38:01.106 INFO 3712 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f51b3e0: startup date [Sat Jun 24 18:37:54 CST 2017]; root of context hierarchy
2017-06-24 18:38:01.328 INFO 3712 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{name}]}" onto public java.lang.String com.example.HelloController.helloName(java.lang.String)
2017-06-24 18:38:01.330 INFO 3712 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.example.HelloController.helloWorld()
2017-06-24 18:38:01.335 INFO 3712 --- [ 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)
2017-06-24 18:38:01.337 INFO 3712 --- [ 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)
2017-06-24 18:38:01.446 INFO 3712 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-24 18:38:01.448 INFO 3712 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-24 18:38:01.586 INFO 3712 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-24 18:38:01.931 INFO 3712 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-06-24 18:38:02.190 ERROR 3712 --- [ main] o.a.coyote.http11.Http11NioProtocol : Failed to start end point associated with ProtocolHandler ["http-nio-8080"]

java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_77]
at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_77]
at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_77]
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_77]
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_77]
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:978) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:628) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1022) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:250) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:193) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at com.example.SpringBootSampleHelloworldApplication.main(SpringBootSampleHelloworldApplication.java:10) [classes/:na]

2017-06-24 18:38:02.191 ERROR 3712 --- [ 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.15.jar:8.5.15]
at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:250) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:193) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at com.example.SpringBootSampleHelloworldApplication.main(SpringBootSampleHelloworldApplication.java:10) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat"; Protocol handler start failed
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1029) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
... 13 common frames omitted
Caused by: java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_77]
at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_77]
at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_77]
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_77]
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_77]
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:978) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:628) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.connector.Connector.startInternal(Connector.java:1022) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
... 14 common frames omitted

2017-06-24 18:38:02.225 INFO 3712 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-06-24 18:38:02.281 INFO 3712 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with \'debug\' enabled.
2017-06-24 18:38:02.328 ERROR 3712 --- [ 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.

2017-06-24 18:38:02.334 INFO 3712 --- [ main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f51b3e0: startup date [Sat Jun 24 18:37:54 CST 2017]; root of context hierarchy
2017-06-24 18:38:02.345 INFO 3712 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

----------------------------------------------------------------------------------------------------------------------

好吧,这是我写的第一篇开发博客,写得罗里吧嗦的,但感觉还是挺好玩的,另外怎么感觉我总是写得语无伦次,满口胡言乱语呢。。不管了,期待后面的博文能写好点吧。

 

以上是关于初学SpringBoot之一的主要内容,如果未能解决你的问题,请参考以下文章

springboot_03_初学注意事项

超适合初学者的SpringBoot入门学习笔记,收藏起来慢慢看!

初学SpringBoot之三

初学SpringBoot之二

springboot入门之一:环境搭建(续)

springboot初学---使用redis