第一个spring boot应用

news/2024/7/4 11:48:45

前提

首先要确保已经安装了java和maven:

$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_102, vendor: Oracle Corporation

创建POM

需要创建Maven的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>com.example</groupId><artifactId>myproject</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.BUILD-SNAPSHOT</version></parent><!-- Additional lines to be added here... --><!-- (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>

此时就可以将项目导入IDE了。

添加Classpath依赖

Spring Boot提供了一些“Starters”让用户将jar包添加到classpath。上面给出的样例已经包含了spring-boot-starter-parent依赖。

因为我们要开发一个WEB应用,所以引入spring-boot-starter-web依赖。在加入之前我们可以通过如下命令来看现在已经有哪些依赖:

$ mvn dependency:tree[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

 

mvn dependency:tree命令将项目的依赖以树的形式表现出来,可以看到spring-boot-starter-parent自身并不提供依赖。

将spring-boot-starter-web依赖添加到pom.xml文件的parent部分:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

写代码

创建src/main/java路径,添加Example.java到路径中:

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);}}

 @RestController and @RequestMapping 注解

例子中的第一个注解是@RestController,Spring通过该注解知道这个类扮演某个具体的角色。

我们的类是一个Web @Controller,所以Spring认为它处理Web请求。

@RequestMapping提供路由信息,它告诉Spring所有以/为路径的HTTP请求都应该被映射到home方法。

@RestController告诉Spring将结果字符串传递给调用者

@EnableAutoConfiguration注解

告诉Spring通过你的依赖来猜测你要开发什么样的应用,因为spring-boot-starter-web添加了tomcat和Spring-MVC,

auto-configuration推断你要开发一个Web应用,并相应的建立Spring。

The“main” Method

main方法通过调用run代理了Spring Boot的SpringApplication

运行程序

$ mvn spring-boot:run.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::  (v2.0.0.BUILD-SNAPSHOT)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)

然后访问localhost:8080

创建可执行jar包

为了创建可执行jar包,需要将spring-boot-maven-plugin加入到pom.xml中(添加到dependency部分下面):

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

保存pom.xml,运行mvn package:

$ mvn package[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.0.BUILD-SNAPSHOT:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

现在打开target目录,会有myproject-0.0.1-SNAPSHOT.jar.original文件和myproject-0.0.1-SNAPSHOT.jar文件。

最后可以通过java -jar运行。

转载于:https://www.cnblogs.com/jugglee/p/8036354.html


http://lihuaxi.xjx100.cn/news/239475.html

相关文章

刷新mysql的权限相关表_flush privileges刷新MySQL的系统权限相关表

mysql> flush privilegesmysql> update mysql.user set passwordPASSWORD(‘新密码’) where User’root’;mysql> flush privileges;mysql> quit答:mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表&#xff0c;否则会出现拒绝访问&am…

linux的挂载命令

在linux中所有的存储设备都必须挂载后才能使用&#xff0c;相当于windows的分配盘符 挂载命令 mount #查看系统中已经挂载好的设备 mount -a #根据/etc/fstab中的内容&#xff0c;自动挂载 /etc/fstab是系统开机的自动挂载文件 系统挂载时要自动检车测这个文件&#xff0c;如果…

mysql中日期判断的函数_MySql判断汉字、日期、数字的函数

几个平常用的mysql函数 /***************************************************** 1.判断字符串是否为汉字 返回值&#xff1a;1-汉字 0-非汉字 *****************************************************/ DROP FUNCTION IF EXISTS fc_is_hanzi; CREATE FUNCTION fc_is_h几个平常…

ecmascript_TC39及其对ECMAScript的贡献

ecmascriptby Parth Shandilya通过Parth Shandilya TC39及其对ECMAScript的贡献 (TC39 and its contributions to ECMAScript) Many people get confused about what is JavaScript and what is ECMAScript. Sometimes it’s hard to tell how they are connected with each o…

vsCode

Q: 修改vscode的默认集成终端&#xff1a; "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 转载于:https://www.cnblogs.com/maoriaty/p/8039296.html

[Ubuntu] ubuntu10.04系统维护之Wine的安装

在介绍安装wine之前&#xff0c;我想是有必要先介绍一下Wine的。当然&#xff0c;如果是Liunx的高手&#xff0c;我想是没必要看的&#xff0c;但是对于笔者这样的菜鸟级人物还是需要看一下的。 Wine是一款Liunx下的模拟器软件&#xff0c;但是Wine又不仅仅是一个模拟器软件&am…

拨测工具_您可以拨多少钱? 快速简单地介绍有用的工具。

拨测工具by Miguel Bustamante通过Miguel Bustamante 您可以卷曲多少&#xff1f; 快速简单地介绍有用的工具。 (How much can you cURL? A quick and easy intro to a useful tool.) On a good day I can flex a 20 lb weight…twice. Probably. But that’s not the type o…

oracle重建实例_记一次误删Oracle控制文件并恢复过程

概述当你在数据库运行时误删除了控制文件怎么办&#xff1f;很不幸有一次我就有这个情况,虽然是测试环境&#xff0c;这里因为我有事先把控制文件分别备份&#xff0c;所以恢复还是比较简单的。下面简单记录下怎么恢复。问题控制文件版本不一致一般是因为在实例运行时删除了控制…