Springboot项目简单的启动

1.首先我们为什么使用springboot?

        我们最开始时候使用的是本机的main程序,发现运行后就停止了运行只能写一些简单的逻辑,无法持续的运行。后面我们使用了servelt框架,我们发现使用了这个框架后可以持续的运行,因为tomcat是一个服务器,我们发现用了tomcat就可以访问到http请求,因为tomcat对于servelt进行了再次的封装,但是我们发现servelt很麻烦,不管是请求还是获取参数都很繁琐,后面我们使用了springmvc,springmvc依赖于spring我们发现很方便,但是还有一个问题,一些繁琐的配置让我们很是反感,这个时候springboot出现了。

2.springboot的简单介绍

        Spring Boot是由Pivotal团队提供的基于Spring的框架,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。Spring Boot集成了绝大部分目前流行的开发框架,就像Maven集成了所有的JAR包一样,Spring Boot集成了几乎所有的框架,使得开发者能快速搭建Spring项目。

3.springboot的核心思想

        springboot核心思想就在于约定大于配置,或者说约定优于配置,我们使用springboot一定要知道这个思想,使用过springboot的同学发现了,我们写的配置文件要么就是application.yml或者applition.properties来进行配置我们的项目,明明我也没有告诉我的springboot我的配置文件叫什么,但是我就能在这里面书写我的配置,这就是springboot给我们的约定,而我们不用再配置告诉springboot我的配置文件在哪里,因为我们默认就会有个resources文件夹,而我们的配置文件就在这里。这个是springboot告诉我们的,而不是我们告诉springboot的,这就是约定优于配置体现,我们不用再写一些繁琐的代码,就是为了简化我们的开发。

4.springboot项目启动

我们选择maven进行构建项目 

 

 我们发现构建后大概成这个样子刚刚提到的resources就可以放我们的配置文件,等下我们导入springboot依赖,导入springboot依赖之前我们先做一些基础的配置。

我们选择seeting然后搜索maven,点击重写设置我们的maven的seetting文件会自动映射我们的本地仓库,选择后我们等下导入的依赖就会导入到我们的本地仓库中。

我一般比较喜欢手动搭建spring项目,所以我会把其余的配置文件删掉,自己来搭建项目,我们也可以删除配置文件,然后重新搭建。

 

我们导入如下的依赖,我们发现我们的spring-boot-starter-web并没有导入版本号是为什么呢,等下我们继承的spring-boot-parent看一下。

我们发现spring-boot-parent继承了spring-boot-dependencies,那我们再点进去看一下。

我们发现和依赖项导入的版本号一模一样,说明spring-boot-parent帮我们已经把版本依赖关系进行了管理,我们可以直接导入这个spring-boot-starter-web依赖,不用关心版本。

 

我们右键main,然后选择java,我们创建java目录然后写一个简单的启动类。 

 

我们在启动类加上@springbootapplication注解

我们写一个简单的可访问的接口,然后启动看看是否能访问成功。 

我们发现访问成功,但是有一个疑问我们为什么可以通过web来访问,我们明明没有用到tomcat,怎么会访问成功,但是我们发现端口就是tomcat的默认端口8080,那我们查看一下依赖项。 

我们发现原来springboot内嵌了tomcat,我们不用在idea嵌入tomcat,也不用在启动tomcat了,是不是很方便呢,原来一些繁琐的东西springboot都帮我们进行了简化。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适合初学 springboot 的同学 --------------------------- maven配置:pom.xml --------------------------- <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> --------------------------- src/main/resources/application.yml --------------------------- spring: # 指定静态资源的路径 resources: static-locations: classpath:/static/,classpath:/views/ thymeleaf: prefix: classpath:/templates/ server: port:8080 context-path:"/" --------------------------- DemoApplication 运行 main 方法即可启动 springboot --------------------------- package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.example.*"}) //指定扫描包路径 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class HelloWorldController { // http://localhost:8080/hello 返回的是文本"Hello World" @RequestMapping("/hello") public String index() { return "Hello World"; } /** * 本地访问内容地址 :http://localhost:8080/hello3 ;返回的是文本 */ @RequestMapping("/hello3") public String helloHtml(HashMap<String, Object> map) { map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SpringBootController { /** * 本地访问内容地址 :http://localhost:8080/hello2 ; 可访问到 * src/main/resources/views/index.html */ @RequestMapping("/hello2") public String helloHtml(HashMap<String, Object> map) { // 传参数hello到html页面 map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- src/main/resources/views/index.html --------------------------- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> aaaaaaaaaaaaaaaaaaaaaaacccccccccccccc <p th:text="${hello}">dddd</p> </body> </html> --------------------------- 直接访问静态页面 --------------------------- http://localhost:8080/index.html 可直接访问到 src/main/resources/templates/index.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值