您的位置:首页 > 编程语言 > Java开发

Springboot 第一次体验

2016-12-29 01:34 337 查看

Springboot 第一次体验

本文主要是老司机开车带入门的第一课,会介绍springboot大概功能,接着演示一个helloworld并跑起来。

springboot能做什么

第一个springboot程序 Helloworld

结语

springboot能做什么

关于这个,官网有一段介绍,翻译一下大概是下面的意思,

springboot是一个基于springframework周边产品的快速构建产品级应用的‘神器‘(我是迷弟)。。。她需要最少的配置,抢到好处的依赖封装,能让你更少的遇到坑。

特性

– 构建独立应用程序,很酷吧

– 内嵌tomcat,jetty 等服务器,听起来很吊,是不?

– 提供了恰当的starter pom简化maven配置,这里是包依赖层次的简化,后面再看。

– 提供了可用于生产的特性,比如性能指标,健康监控和配置对外开放(这里应该是提供了更多让外部调用者自己配置的可能)

– 完全没有代码生成以及多余的XML配置

好吧,说这么多,反正很吊就对了。。。

这里是原文 -_-

Create stand-alone Spring applications

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

Provide opinionated ‘starter’ POMs to simplify your Maven configuration Automatically configure

Spring whenever possible Provide production-ready features such as metrics, health checks and externalized configuration

Absolutely no code generation and no requirement for XML configuration

开车了,HelloWorld走起

说什么都没用,直接撸代码吧!

假设我们使用maven来组织构建项目,第一步新建一个maven project

先来看一个pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>learn.springboot</groupId>
<artifactId>helloworld</artifactId>
<version>1.0-SNAPSHOT</version>

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.4.RELEASE</version>
</parent>

<!-- let's develop a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Spring Boot Maven Plugin, it's used for repackage/ run springboot app -->

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


这里我们可以看到,引入了spring-boot-starter-parent这个依赖。

她是用来提供依赖和插件管理的,通过继承方式自动引入了父级依赖和插件,这样可以简化springboot 应用的构建,更多精力关注应用程序的业务逻辑开发。

先来看一个我们的应用代码

### Application.java

package learn.springboot.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Application {

@RequestMapping("/")
public String home() {
return "Hello, this is the first web app";
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


代码写完了,卧槽,是不是很快速,很难想象一个web应用就这样开发完了。

总体上是这样的。



执行一次maven build构建项目。

好了,现在开车上路

启动一个java小程序



打开浏览器看看我们开发好的web应用

不作修改,默认是打开 localhost:8080 查看效果。



结语

springboot是不是很神奇?

细心的读者会发现启动过程的日志里打印了TomcatEmbed,其实她帮我们把服务器内嵌了。启动服务器后加载了我们的web工程。这里是做了一个Rest API指向了根域名。后面我会对springboot做更多深入的介绍。

先讲到这里代码可以github: https://github.com/levinliu/springboot-helloworld

Springboot 第一次体验
springboot能做什么

开车了HelloWorld走起
pomxml

启动一个java小程序

打开浏览器看看我们开发好的web应用

结语

[1]: Spring-boot官网,点击这里 >> http://projects.spring.io/spring-boot/

[2]: 博主的github 猛戳 >> https://github.com/levinliu/springboot-helloworld
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springboot