Spring Boot创建一个HelloWorld项目
2024-08-27 23:44:41
Spring Boot 简介
Spring Boot 是所有基于 Spring 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。SpringBoot不是什么新的框架,它只是默认配置了很多框架的使用方式。
Spring Boot设计目的是用来简化spring应用的初始化环境搭建以及开发过程。 嵌入tomcat服务器,无需部署war文件。
Spring Boot并不是spring功能的增强,而是提供了一种快速使用spring的方式。
说明:jdk1.7 使用spring boot 2.0以下版本, jdk1.8 使用spring boot 2.0以上版本
微服务框架
Spring Boot
Spring Data
Spring Cloud
以前使用spring开发web的方式
1:创建web项目,导入相关jar包
2:创建web.xml文件,创建application.xml,springmvc.xml配置文件
3:编写控制器Controller
4:需要部署web项目到服务器 tomcat
开发起来比较麻烦
Spring Boot 启动器介绍
1.Spring Boot 启动器其实就是一个jar包集合
2.spring boot将很多的jar包放入到不同的启动器中, 用什么启动器,就注入对应启动的jar包。
3.Spring boot 一共提供了44个启动器。常用的有:
- spring-boot-starter-web
支持全栈是的web开发(web项目开发)
包括:tomcat和spring springmvc 等jar - spring-boot-starter-jdbc
支持spring以jdbc方式操作数据库的jar包集合 - spring-boot-starter-redis
支持redis数据库操作的jar - spring-boot-starter-test
支持常规的测试依赖的jar包括junit,spring-test的jar等 - spring-boot-starter-log4j
支持log4j日志框架jar - spring-boot-starter-aop
支持面向切面编程的jar,包括spring-aop,apectj等
如何创建一个helloword的SpringBoot项目
- 创建 Maven project 项目(IDEA)
不用勾选创建骨架,点下一步Next - 根据个人需求配置groupid、Artifact、version
- 选择项目存放目录
- 打开pom.xml引入依赖启动器
<!--引入springboot父项目依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<!--引入springboot web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 在/src/main/java目录下创建包 新建springboot的启动类
代码如下:
@SpringBootApplication //springboot启动类的注解 判断此项目是springboot项目
public class SpringBootApplicationRun {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationRun.class,args);
}
}
- 创建前端控制器controller ,由于是springboot项目,所以不需要配置web.xml、springmvc.xml
代码如下:
@RestController //@RestController= @@Controller + @ResponseBody
public class controller {
@RequestMapping("/helloword")
public String helloword(){
return "helloword";
}
}
注意启动类和控制器类的位置: :
启动类和控制器可以位于同一个包下,或启动类位于控制器上一级包下。
但是启动类不能放在控制器的平级包或子包下。
原因:启动器启动时从当前包下以及子包下查找使用的组件。(上级包或不同包的话无法找到)
- 运行springboot启动类 浏览器输入 localhost:8080/helloword
点击SpringBootApplicationRun
最新文章
- 剖析javascript全局变量和局部变量
- MSSQL—列记录合并
- web view调h5的方法死活调不到
- CentOS 6.5 3.0.4安装agentd
- 如何提取HTML代码中img的src地址?
- 支持异步通知的globalfifo平台设备驱动程序及其测试代码
- 理解$watch ,$apply 和 $digest --- 理解数据绑定过程
- shell编程的一些例子3
- bootstrapValidator Maximum call stack size exceeded
- [POJ] 1064 Cable master (二分查找)
- redolog
- 【ThinkingInC++】66、pointer Stash的使用
- Objective-C运行时态消息传递--拼接方法名
- hibernate事务控制
- 使用rsync无密码传输
- web前端开发工程师工资多少
- 黄金K线理论简述
- zabbix 监控进程
- sql server 性能调优之 资源等待 LCk
- Python 同一文件中,有unittest不执行“if __name__ == &#39;__main__”,不生成HTMLTestRunner测试报告的解决方案
热门文章
- 前端ps中常用的操作
- Web实验一 国内旅游界面
- mac笔记本编译go-ethereum报错CoreServices/CoreServices.h&#39; file not found
- Python xlsxwriter模块
- python基础学习day02
- [译]ABP框架v2.3.0已经发布!
- Deep Protein Methylation Profiling by Combined Chemical and Immunoaffinity Approaches Reveals Novel PRMT1 Targets (结合层析法和免疫沉淀法的蛋白甲基化的深度检测技术发现了PRMT1新的靶标蛋白)
- jenkins-gitlab-harbor-ceph基于Kubernetes的CI/CD运用(一)
- 使用SpringBoot + JavaMailSender 发送邮件报错 Mail server connection failed;Could not connect to SMTP host
- 常用的FTP命令