以前用得多是spring framework,现在试用spring-boot,hello world过程如下(只是简单的helloworld,实用见后续文章):
1)准备eclipse,JDK(我的版本是1.7),maven 3(端口版本:http://tomcat.apache.org/whichversion.html)
2)window中的Java设置,即JAVA_HOME / PATH / CLASSPATH;window中的maven设置,即M2_HOME / MAVEN_HOME / PATH
3)eclipse中的maven设置,即eclipse > Preferences > Maven > Installations 及 User Settings
4)eclipse > new > maven > maven project,maven-archetype-quickstart, Group Id: User, Artifact Id: login, Package: com.company.controller
5)maven的setting.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>E:/repository</localRepository>
<profiles>
<profile>
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
6)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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>User</groupId>
<artifactId>login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>login</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable JAR -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7)HelloWorld.java
package com.company.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class HelloWorld {
@RequestMapping("/sayHello")
@ResponseBody
public String sayHello(){
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class, args);
}
}
8)pom.xml > run as > run configuration > maven build > name: (any), goals: spring-boot:run,勾skip tests,如果想改默认port端口8080,可以在VM arguments设置:-Dserver.port=8181,如图:
9)运行:pom.xml > run as > maven build
10)试验:http://localhost:8181/sayHello
结论:
a)maven会引入spring-boot的包,不用另外下载引入
b)spring-boot调用启动内嵌tomcat 7.0(见maven dependencies)
参考:
http://spring.io/blog/2014/03/07/deploying-spring-boot-applications
https://dzone.com/articles/spring-boot-support-spring
http://jinnianshilongnian.iteye.com/blog/1997192
http://www.infoq.com/cn/news/2014/01/spring-boot
http://docs.spring.io/spring-boot/docs/1.2.0.BUILD-SNAPSHOT/maven-plugin/examples/run-debug.html