Spring boot 入门(一)环境搭建以及第一个应用

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(13066)   2023-03-28 11:29:14

一.Spring boot与spring MVC的关系

两者没有必然的联系,但是spring boot可以看作为spring MVC的升级版

二.SpringBoot的特点

1.化繁为简,简化配置(最大特点)
2.备受关注,是下一代spring框架
3.微服务的入门级微框架

三.环境准备

打开eclipse安装spring的工具插件STS
安装方法:
1.电脑连接网络
2.依次选择eclipse的 help >Eclipse Marketplace
3.搜索STS
eclipse STS插件
安装上图勾选的STS
注意:安装过程中电脑需要连接网络

四.创建第一个spring boot项目

1.新建项目,选择Spring Starter Project
创建项目
2.填写项目相关信息
项目信息
我默认的

3.选择Spring Boot版本以及依赖完成项目创建
版本选择的1.51,依赖选择了一个web,点击finish完成项目的创建
版本以及依赖

4.项目结构
项目结构
5.删除一些不需要的文件
 1>删除 mvnw 
 2>删除 mvw.cmd
6.项目说明
1.默认有个DemoApplication类,里面是spring boot的载入函数
2.resource目录下有个application.properties文件,这个是Spring boot的配置文件
3.test目录下有个测试类DemoApplicationTests,这个是spring boot的单元测试
4.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>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>1.5.1.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-web</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>
注意观察
一个继承spring-boot-starter-parent,两个依赖,spring-boot-starter-web web项目依赖必须,spring-boot-starter-test spring boot项目单元测试依赖

7.启动项目
通过spring boot的启动类,这里是DemoApplication
启动
选择Run As > Spring Boot App
启动完成
上面标示启动完成,
tomcat启动在8080端口,http协议,启动花费了1.707秒
打开浏览器,输入地址:
错误
 出现上图404错误是正常的,因为我们什么都没写

8.编写一个HelloController
package com.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
 * create leftso.com
 */
@RestController
public class HelloController {

	@RequestMapping(value="/hello",method=RequestMethod.GET)//写法与springMVC有点相似
	public String say(){
		return "Hello Spring Boot!";
	}
}

重启应用
hello映射
重启发现刚才写的hello已经映射出来了
访问/hello
/hello
这时候已经出来我们写的内容了


Spring Boot 第一个项目hello就完成了。

推荐:

spring boot入门(二)属性配置
spring boot入门(三)controller的使用
spring boot入门(四)数据库操作

 

地址:https://www.leftso.com/article/64.html

相关阅读

spring boot又一个spring框架的经典项目,本文讲解spring boot入门的环境配置以及第一个项目,Spring Boot 入门教程
spring boot入门,spring boot是一个崭新的spring框架分支项目,本文讲解其属性配置相关
spring boot是一个崭新的spring框架分支项目,本文讲解基本的数据库配置
spring boot是一个崭新的spring框架分支项目,本文讲解spring boot中controller的常用注解使用
Spring Boot 入门 AOP 日志处理,这里主要讲解spring boot 中采用AOP方式进行日志的统一处理。spring 框架的处理也是一样。综合来讲,本文主要讲解了spring b...
Spring boot 入门之CORS 跨域配置详解,spring 跨域配置详解。
spring boot 入门 使用spring.profiles.active来分区配置,,在spring boot中可以存在多个环境的配置文件通过配置spring.profiles.activ...
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
spring boot 入门之整合spring session实现session共享。一直以来Java编程中web项目中的session共享问题都是一个很难解决的问题。接下来将讲解通过sprin...
spring boot mybatis 整合使用讲解介绍,spring boot与mybaties的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个...