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 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...
随机推荐
- Apache基于域名、端口、IP的虚拟主机配置(Centos 6.5)
虚拟主机:部署多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,不同的ip,需要虚拟主机功能.一句话,一个http服务要配置多个站点,就需要虚拟主机. 虚拟主机分类:基于域名.基于端口 ...
- Python3 实例
一直以来,总想写些什么,但不知从何处落笔. 今儿个仓促,也不知道怎么写,就把手里练习过的例子,整理了一下. 希望对初学者有用,都是非常基础的例子,很适合初练. 好了,Follow me. 一.Pyth ...
- 【转载】解密ThreadLocal
概述 相信读者在网上也看了很多关于ThreadLocal的资料,很多博客都这样说:ThreadLocal为解决多线程程序的并发问题提供了一种新的思路:ThreadLocal的目的是为了解决多线程访问资 ...
- Python函数Day2
一.函数补充 只有一个参数时,变量用argv 二.动态参数 为了拓展,对于传入的实参数量不固定,需要万能参数,即动态参数 *args **kwargs 在函数定义时,在 *args为位置参数,起聚合的 ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
- python算法与数据结构-常用查找算法一(37)
一.什么是查找 查找(Searching)就是根据给定的某个值,在查找表中确定一个其关键字等于给定值的数据元素(或记录). 查找表(Search Table):由同一类型的数据元素(或记录)构成的集合 ...
- NodeJS开发博客(三) 数据的保存
什么是cookie 存储在浏览器的一段字符串(最大5k) 跨域不共享 格式如 k1=v1 k2=v2 因此可以存储结构化数据 每次发送http请求,会将请求域的cookie一起发送给server se ...
- java中创建对象的方式
Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 使用new关键字 } → 调用了构造函数 使用Class类的newInstance方法 } → 调用了构造函数 使用Construc ...
- vue 单向数据流
- POJ-2065-SETI(高斯消元)
链接: https://vjudge.net/problem/POJ-2065 题意: For some years, quite a lot of work has been put into li ...