前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot系列之-helloword入门

Spring Boot系列之-helloword入门

作者头像
编程随笔
发布2019-09-11 15:27:15
3170
发布2019-09-11 15:27:15
举报
文章被收录于专栏:后端开发随笔

一. What: Spring Boot是什么? 以1.4.3.RELEASE为例,官方介绍为:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/html/getting-started-introducing-spring-boot.html

简单来说,使用Spring Boot能够非常方便,快速地开发一个基于Spring框架,可独立运行的应用程序。

具体来说,Spring Boot集成了一些在项目中常用的组件,比如:嵌入式服务器,安全等。而且在使用Spring Boot开发应用程序时,完全不需要XML配置。

二. Why: 为什么Spring Boot能如此易用? //TODO: 随后再详细介绍

三. How: 如何建立一个简单的Spring Boot应用程序?

环境要求: 1. JDK: Although you can use Spring Boot with Java 6 or 7, we generally recommend Java 8 if at all possible. 2. Maven: Spring Boot is compatible with Apache Maven 3.2 or above.

示例项目:

1. 新建一个maven项目,并在pom.xml中添加如下依赖配置:

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4 <modelVersion>4.0.0</modelVersion>
 5 
 6 <groupId>org.chench</groupId>
 7 <artifactId>springboot-hellworld</artifactId>
 8 <version>1.0.0</version>
 9 <packaging>jar</packaging>
10 
11 <name>springboot-hellworld</name>
12 <url>http://maven.apache.org</url>
13 
14 <!-- 使用spring boot框架:声明spring boot为父依赖 -->
15 <parent>
16 <groupId>org.springframework.boot</groupId>
17 <artifactId>spring-boot-starter-parent</artifactId>
18 <version>1.4.3.RELEASE</version>
19 </parent>
20 
21 <properties>
22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23 </properties>
24 
25 <dependencies>
26 <!-- 开发web应用 -->
27 <dependency>
28 <groupId>org.springframework.boot</groupId>
29 <artifactId>spring-boot-starter-web</artifactId>
30 </dependency>
31 
32 <dependency>
33 <groupId>junit</groupId>
34 <artifactId>junit</artifactId>
35 <scope>test</scope>
36 </dependency>
37 </dependencies>
38 
39 <build>
40 <plugins>
41 <!-- 打包插件: 可将项目打包为独立执行的jar文件 -->
42 <plugin>
43 <groupId>org.springframework.boot</groupId>
44 <artifactId>spring-boot-maven-plugin</artifactId>
45 </plugin>
46 </plugins>
47 </build>
48 
49 </project>

2. 新建HelloWordApplication:

代码语言:javascript
复制
 1 @RestController
 2 @EnableAutoConfiguration
 3 public class HelloWordApplication {
 4 
 5 @RequestMapping("/")
 6 public String helloworld() {
 7 return "hello,world";
 8 }
 9 
10 public static void main(String[] args) {
11 SpringApplication.run(HelloWordApplication.class, args);
12 }
13 }

3.运行HelloWordApplication: 如果是在eclipse等开发环境中,直接运行: "Run As" -> "Java Application" 。 或者,直接将项目打包为可独立执行的jar文件: mvn clean package,将生成文件: springboot-hellworld-version.jar 。 执行如下命令启动项目: java -jar springboot-hellworld-version.jar

启动之后,在浏览器地址栏输入: http://localhost:8080/

完毕!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-12-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档