# spring boot hello world
## 概念
spring boot 是一个框架,能够让使用者更快速 更少配置地建立一个可用的 可发布的spring应用.
## 安装
spring boot安装依赖
1.jdk 1.8或以上版本
2.这里最佳实践是通过spring.io创建一个spring maven 项目 with spring boot
https://start.spring.io/
![](media/15221185243951/15231735064448.jpg)
直接将生成好的project文件解压后,已经是创建好的maven project了.
./mvnw verify 之后会本地下载和安装一个maven根据网络情况可能要十几分钟
2.pom.xml已经添加好了spring boot的相关依赖
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这里定义了这个pom文件的父类是spring-boot-starter-parent,这是一个比较推荐的方式,当然也可以继承别的父类,然后通过import的方式使用spring boot,官网有介绍
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
spring boot maven 插件支持以jar 包方式发布应用
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring boot 和test 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.src/main/java/com/example/demo 下会有一个 DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
DemoApplication(){
System.out.println("hello world!");
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后执行 ./mvnw package 将会下载相关依赖然后打包,target下有发布包
demo-0.0.1-SNAPSHOT.jar.original是原始应该大小,demo-0.0.1-SNAPSHOT.jar增加了相关依赖要大些
java -jar demo-0.0.1-SNAPSHOT.jar //可以运行应用,打印 hello world!
4.我们还可以建立一个web应用版本的hello world
package com.example.demo
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
pom.xml需要增加web依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
mvnw package之后,java -jar /Users/hbjava1985/Downloads/demo/target/demo-0.0.1-SNAPSHOT.jar 之后 浏览器访问
http://localhost:8080/ 就会出现hello world!
5.为了更方便地使用spring boot,可以去安装 spring boot cli,这个工具可以使用命令行的方式让你使用spring boot
下载之后解压 https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.0.1.RELEASE/spring-boot-cli-2.0.1.RELEASE-bin.zip
bin/spring //执行cli
写一个测试 app.groovy
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
bin/spring run app.groovy
分享到:
相关推荐
SpringBoot 入门Demo源码,只是单纯的分享给有需要的人使用,俗话说的好,万事开头难,当迈开了第一步,就相当于打开了一扇门,发现了新大陆,所以我们要持之以恒,才能取得成功,让我们一起努力,为心中最美好的...
Spring Boot Hello World 入门源代码,直接返回一个http的JSON的输出
博客示例代码,Spring boot 自学笔记(一) hello world 代码
主要介绍了Spring Boot Hello World的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...
springboot的helloworld案例,包括配置jsp视图解析和thymeleaf视图解析,以及同时应用,说明http://blog.csdn.net/qq_36666651/article/details/78776272
springboot-helloworld.rar
Spring Boot Hello index Hello World 内置容器 秒启动
SpringBoot的第一个工程内容是HelloWorld,具体实现过程可以看我的blog,关于SpringBoot的学习(一)
Spring Boot 学习示例 Spring Boot 2.0 Mysql 5.6 ...spring-boot-helloworld:Spring Boot 3.0 Hello World Test 单元测试示例 spring-boot-scheduler:Spring Boot 3.0 定时任务 scheduler 使用示例 .....
1. Spring boot helloworld 2. Spring boot 返回 json 数据 3. Spring boot 使用其他 json 转换框架 4. Spring boot 全局异常捕捉 5. Spring boot JPA 连接数据库 6. Spring boot 配置 JPA 7. Spring boot 整合 ...
博客文章示例代码: spring boot 自学笔记(一) hello word
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简...
Spring Boot学习笔记-------(二)spring boot入门,配套例子代码,博客地址:https://blog.csdn.net/huaya1127/article/details/104130300
这个项目是一个基础的spring-boot整合项目,麻雀虽小,五脏俱全,先提供给需要的人。
第 1-4 课:写一个 Hello World 来感受 Spring Boot/hello 第 2-1 课: Spring Boot 对基础 Web 开发支持/spring-boot-web 第 2-10 课: 使用 Spring Boot WebSocket 创建聊天室/spring-boot-websocket 第 2-2 课...
IDEA JAVA Spring Boot运行Hello World(1.8) 我自己写的文档和建立的工程文件不存在版权问题。
spring boot hello world rest
开篇词:为什么要学 Spring Boot.pdf ...第 1-4 课:写一个 Hello World 来感受 Spring Boot.pdf 第2-1课:Spring Boot 对基础 Web 开发的支持(上).pdf 第 2-1 课:Spring Boot 对基础 Web 开发的支持(下
1.3 spring boot起步之Hello World 1.4 Spring Boot返回json数据 1.5 Spring Boot热部署 1.6 Spring Boot使用别的json解析框架 1.7 全局异常捕捉 1.8 Spring Boot datasource - mysql 1.9 JPA - Hibernate 1.10 使用...