Boot对Spring应用的开发进行了简化,提供了模块化方式导入依赖的能力,强调了开发RESTful Web服务的功能并提供了生成可运行jar的能力,这一切都清晰地表明在开发可部署的微服务方面Boot框架是一个强大的工具。正如前面的例子所示,借助于Boot,让一个RESTful Web工程运行起来是一件很容易的事情;不过,为了了解Boot所有潜在的功能,我们会阐述在开发完整功能的微服务时,会遇到的所有繁琐的事情。在企业级基础设施领域,微服务是一种越来越流行的应用架构,因为它能够实现快速开发、更小的代码库、企业级集成以及模块化部署。
创建第一个Spring Boot应用。
1. 新建一个Maven项目
POM文件如下:
<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>com.jnk</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<url>http://maven.apache.org</url>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
2. 新建一个App.java,作为Spring Boot的入口。
package com.jnk.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @author NonkeyJiang
*
*/
@SpringBootApplication
public class App
{
public static void main(String[] args)
{
SpringApplication.run(App.class, args);
}
}
3. 新建Controller类(Example.java),作为前后台交互的一层。
/**
*
*/
package com.jnk.springboot;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Example
{
@RequestMapping("/")
public String home()
{
return "Hello World!";
}
@RequestMapping("/hello/{myName}")
String index(@PathVariable String myName)
{
return "Hello " + myName + "!!!";
}
}
4. 执行App.java
不管你相不相信,一个最简单的web应用就这样开发完并且部署完成了。在浏览器里面输入url,就可以访问。
真正的轻量级,真正的微服务,我已经深深的被它迷住了,还有许多要学习的,还要继续探索。
参考网址:
http://www.infoq.com/cn/articles/microframeworks1-spring-boot
http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-mustache/