SpringBoot
化繁為簡,簡化配置
SpringBoot官方:http://projects.spring.io/spring-boot/
SpringBoot使用介紹:http://blog.csdn.net/isea533/article/details/50278205
Maven項目使用SpringBoot
建議使用 IDEA ,雖然下面是 Eclipse 測試的例子
配置SpringBoot
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<project xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelversion>
4.0
.
0
</modelversion>
<groupid>com.springboot</groupid>
SpringBootTest</artifactid>
<packaging>war</packaging>
<version>
0.0
.
1
-SNAPSHOT</version>
<name>SpringBootTest Maven Webapp</name>
<url>http:
//maven.apache.org</url>
<properties>
<project.build.sourceencoding>UTF-
8
</project.build.sourceencoding>
<java.version>
1.8
</java.version>
<tomcat.version>
7.0
.
55
</tomcat.version>
</properties>
<parent>
<groupid>org.springframework.boot</groupid>
spring-boot-starter-parent</artifactid>
<version>
1.3
.
0
.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
spring-boot-starter-web</artifactid>
</dependency>
</dependencies>
</project>
|
運行程序
運行main方法
SampleController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package
com.hello;
import
java.util.Date;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
(exclude={DataSourceAutoConfiguration.
class
})
public
class
SampleController {
@RequestMapping
(
"/"
)
String home() {
return
"Hello World!"
;
}
@RequestMapping
(
"/now"
)
String hehe() {
return
"現在時間:"
+ (
new
Date()).toLocaleString();
}
public
static
void
main(String[] args)
throws
Exception {
SpringApplication.run(SampleController.
class
, args);
}
}
|
控制台輸出:
1
2
3
4
5
6
7
8
9
10
11
12
|
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ |
'_ | '
_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.
3.0
.RELEASE)
2016
-
12
-
15
16
:
30
:
13.765
INFO
20952
--- [ main] com.hello.SampleController : Starting SampleController on DESKTOP-QSFD0OC with PID
20952
(D:\eclipse4.
61
\eclipse\workspace\SpringBootTest\target\classes started by Peng in D:\eclipse4.
61
\eclipse\workspace\SpringBootTest)
2016
-
12
-
15
16
:
30
:
13.768
INFO
20952
--- [ main] com.hello.SampleController : No profiles are active
2016
-
12
-
15
16
:
30
:
13.803
INFO
20952
--- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
@149494d8
: startup date [Thu Dec
15
16
:
30
:
13
CST
2016
]; root of context hierarchy
...
|
在瀏覽器上輸入地址http://localhost:8080/
在瀏覽器上輸入地址http://localhost:8080/now
注意事項
Tomcat問題一
在Java Build Path中,不要加入 Tomcat 運行環境的依賴庫,已經在pom.xml中指定了 Tomcat 版本(不指定默認為 Tomcat 8.0.28),從 maven 的依賴包中能找到 Tomcat 的jar包,SpringBoot 自己配有 Tomcat 。
Tomcat問題二
運行 main 方法,出現 Tomcat 不能啟動的錯誤。如下面的異常信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[na:
1.8
.0_91]
at sun.nio.ch.Net.bind(Unknown Source) ~[na:
1.8
.0_91]
at sun.nio.ch.Net.bind(Unknown Source) ~[na:
1.8
.0_91]
org.apache.catalina.LifecycleException: Failed to start component [Connector[org.apache.coyote.http11.Http11NioProtocol-
8080
]]
Caused by: org.apache.catalina.LifecycleException: service.getName():
"Tomcat"
; Protocol handler start failed
at org.apache.catalina.connector.Connector.startInternal(Connector.java:
1014
) ~[tomcat-embed-core-
7.0
.
55
.jar:
7.0
.
55
]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:
150
) ~[tomcat-embed-core-
7.0
.
55
.jar:
7.0
.
55
]
...
13
common frames omitted
Caused by: java.net.BindException: Address already in use: bind
Unable to start embedded Tomcat servlet container
Caused by: java.lang.IllegalStateException: Tomcat connector in failed state
|
原因是你運行了程序一次,javaw.exe 會占用你的端口,可以結束該進程。重新運行 main 方法