spring boot 之 如何创建spring boot项目
创建spring boot的方式有非常多,今天我们使用maven来进行创建spring boot项目,因为maven使用的非常广泛,也很好用,很多IDE也都支持maven。
1 创建maven项目
1.1 使用eclipse创建maven项目
(1)File —>New —> Project...
(2)选中Maven Project ,点击下一步
(3)默认设置 点击下一步
(4)继续点击 下一步
(5)如下图设置Group Id(组织Id/项目包名) 和Artifact Id(项目名称或者模块名称),然后点击Finish。到此我们就完成了用eclipse创建maven项目。
1.2 使用IntelliJ IDEA 创建maven项目
(1)File —> New —>Project
(2)选中maven点击下一步
(3) 如下图设置Group Id(组织Id/项目包名) 和Artifact Id(项目名称或者模块名称),然后点击下一步。
(4)IDEA会根据上一步的设置默认project name和project location,如果不需要变更则点击Finish。
(5)到此为止,我们就完成了使用IntelliJ IDEA创建maven项目。
2 项目构建
2.1 添加maven依赖
(1)在pom文件中添加以下依赖:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.4.RELEASE</version>
- </parent>
spring-boot-starter-parent 是一个特殊的starter,主要是给项目提供一些默认配置:
- Java版本默认使用1.8;
- 编码格式默认使用utf-8;
- 提供了Dependency Management进行项目依赖的版本管理;
- 默认资源过滤和插件配置。
(2)我们现在要开发一个web项目,则需要引入一个web的starter的依赖:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
2.2 编写启动类
(1)在maven工程的java目录下创建包名为com.sunshine的包
(2)在上面的包下面创建一个 Application的类,并给Application类添加 @EnableAutoConfiguration 注解,如下所示:
- @EnableAutoConfiguration
- public class Application {
- public static void main(String[] args){
- SpringApplication.run(Application.class,args);
- }
- }
- @EnableAutoConfiguration ,顾名思义,就是开启自动化配置。我们在设置pom依赖的时候添加了 spring-boot-starter-web依赖,所以开启自动化配置之后,会自动进行spring和spring MVC配置。
- 在Java的main方法中,通过SpringApplication的 run()方法启动项目,第一个参数是Application.class,告诉spring是哪个主要组件,第二个参数是运行时输入的其他参数。
(3)创建spring MVC的controller,我们现在com.sunshine包下面创建包名为 controller 的包,在该包下面创建名为DemoController的controller类。如下所示:
- @RestController
- public class DemoController {
- @GetMapping("/demo")
- public String demo(){
- return "Hello,this is a spring boot demo.";
- }
- }
(4)因为我们创建的一个DemoController类,在该类下创建了一个“/demo”接口,我们需要把DemoController注册到spring MVC容器中,则需要在启动类Application上面添加注解 @ComponentScan进行扫描包扫描,代码如下:
- @EnableAutoConfiguration
- @ComponentScan
- public class Application {
- public static void main(String[] args){
- SpringApplication.run(Application.class,args);
- }
- }
在这里需要注意的是 @ComponentScan注解只能扫描启动类所在的包下面的类文件,所以通常我们都把启动类放在根包路径,比如我们现在的根包路径就是com.sunshine。
我们也可以使用 @SpringBootApplication注解代替 @EnableAutoConfiguration和@ComponentScan两个注解,如下所示:
- @SpringBootApplication
- public class Application {
- public static void main(String[] args){
- SpringApplication.run(Application.class,args);
- }
- }
2.3 启动项目
(1)在启动类中直接运行main方法即可,如下图所示就是启动成功了:
(2)在浏览器中输入http://localhost:8080/demo进行访问我们刚刚编写的"/demo"接口,效果如下图所示:
3 总结
到此为止,已经完成创建spring boot项目。
- 首先用eclipse或IntelliJ IDEA创建maven项目;
- 其次添加spring boot项目的maven依赖:添加默认配置用的spring-boot-starter-parent 依赖和 web项目的 spring-boot-starter-web依赖;
- 在maven项目下的java目录创建根路径包,在包下面创建启动类 Application,并添加@EnableAutoConfiguration和@ComponentScan 或@SpringBootApplication注解,用于开启自动配置和包扫描;
- 创建controller(控制器)类DemoController,并编写"/demo"接口,记得在DemoController上面添加@RestController注解。在接口方法上添加@GetMapping("/demo"),并指定访问接口路径;
- 允许启动类main方法,并在浏览器访问验证。
如果有什么问题欢迎留言指出,如果有什么疑问欢迎留言评论。
spring boot 之 如何创建spring boot项目的更多相关文章
- Spring入门案例 idea创建Spring项目
spring入门案例 idea创建spring项目 Spring介绍 Spring概述 Spring是一个开源框架,Spring是2003年兴起的轻量级java开发框架,由Rod Johnson 在其 ...
- [转]通过Spring Boot三分钟创建Spring Web项目
来源:https://www.tianmaying.com/tutorial/project-based-on-spring-boot Spring Boot简介 接下来我们所有的Spring代码实例 ...
- 用spring tool suite插件创建spring boot项目时报An internal error occurred during: "Building UI model". com/google/common/
本文为博主原创,未经允许不得转载 在用spring tool suite创建spring boot项目时,报一下异常: 查阅很多资料之后发现是因为装的spring tool suite的版本与ecli ...
- Spring入门(一):创建Spring项目
本篇博客作为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA创建第一个Spring项目以及通过一个示例 ...
- Spring Boot 框架 - 快速创建Spring Boot应用
使用Spring的项目创建向导创建一个Spring Boot项目 创建完成目录 目录文件说明: 主启动程序已生成 resources文件夹中目录结构 static:保存所有的静态资源,例如js,css ...
- 创建Spring Boot项目的几种方式总结
一.我们可以使用Spring Initializr来创建SpringBoot项目. Spring Initializr从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构.虽 ...
- 【spring】1.2、Spring Boot创建项目
Spring Boot创建项目 在1.1中,我们通过"Spring Starter Project"来创建了一个项目,实际上是使用了Pivotal团队提供的全新框架Spring B ...
- Spring学习(九)--Spring的AOP
1.配置ProxyFactoryBean Spring IOC容器中创建Spring AOP的方法. (1)配置ProxyFactoryBean的Advisor通知器 通知器实现定义了对目标对象进行增 ...
- [转] 使用Spring Boot和Gradle创建项目
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...
随机推荐
- jquery事件委托详解
jQuery事件委托处理流程 上一章分析jQuery.event.add的时候已经分析了事件绑定,再把绑定的部分源码抽出来 if ( !(eventHandle = elemData.handle) ...
- 再见 Docker,是时候拥抱下一代容器工具了
本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章和 ...
- PAT基础级-钻石段位样卷2-7-4 6翻了 (15 分)
“666”是一种网络用语,大概是表示某人很厉害.我们很佩服的意思.最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思.如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字 ...
- linux网络编程之posix共享内存
今天继续研究posix IPC对象,这次主要是学习一下posix共享内存的使用方法,下面开始: 下面编写程序来创建一个共享内存: 编译运行: 那posix的共享内存存放在哪里呢?上节中学的posix的 ...
- 线性查找与二分查找(python)
# -*- coding: utf-8 -*- number_list = [0, 1, 2, 3, 4, 5, 6, 7] def linear_search(value, iterable): f ...
- k8s的监控
监控 1.资源指标和资源监控 一个集群系统管理离不开监控,同样的Kubernetes也需要根据数据指标来采集相关数据,从而完成对集群系统的监控状况进行监测.这些指标总体上分为两个组成:监控集群本身和监 ...
- 如何在linux系统下查看日志
在linux系统下, 首先在idea中使用clean---->install----->package将这个项目进行打包,打包的方式 , 根据你在项目中的pom文件,最上面,可以查看到 这 ...
- vueRouter history模式下 nginx配置
对于VUE的router[mode: history]模式(这里主要是为了去除链接上的"#")开发的时候,一般都不出问题.是因为开发时用的服务器为node,Dev环境中已配置好了, ...
- Robot Framework--连接Mysql数据库
1.安装Database-Library 输入命令:pip install robotframework_databaselibrary 2.添加Database的Library 3.实例 *** T ...
- Oracle LOB 大对象处理
LOB类型列主要是用来存储大量数据的数据库字段,最大可以存储4G字节的非结构化数据. 一.LOB数据类型分类 1.按存储数据的类型分: ①字符类型: CLOB:存储大量 单字节 字符数据. N ...