. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.0.RELEASE)
SpringBoot
化繁为简,简化配置
SpringBoot官方:http://projects.spring.io/spring-boot/
SpringBoot使用介绍:http://blog.youkuaiyun.com/isea533/article/details/50278205
Maven项目使用SpringBoot
建议使用 IDEA
,虽然下面是 Eclipse
测试的例子
配置SpringBoot
pom.xml
<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>
<artifactId>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>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
运行程序
运行main方法
SampleController.java
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);
}
}
控制台输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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 。
可以参考:http://blog.youkuaiyun.com/zhang168/article/details/51423905
Tomcat问题二
运行 main 方法,出现 Tomcat 不能启动的错误。如下面的异常信息
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 方法