当前位置:   article > 正文

SpringBoot(一):简单的SpringBoot项目的实例_springboot例子

springboot例子

简单的一个SpringBoot的项目:

1.SpringBoot的系统版本要求:

2.Maven的设置:

  1. <mirrors>
  2. <mirror>
  3. <id>nexus-aliyun</id>
  4. <mirrorOf>central</mirrorOf>
  5. <name>Nexus aliyun</name>
  6. <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  7. </mirror>
  8. </mirrors>
  9. <profiles>
  10. <profile>
  11. <id>jdk-1.8</id>
  12. <activation>
  13. <activeByDefault>true</activeByDefault>
  14. <jdk>1.8</jdk>
  15. </activation>
  16. <properties>
  17. <maven.compiler.source>1.8</maven.compiler.source>
  18. <maven.compiler.target>1.8</maven.compiler.target>
  19. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  20. </properties>
  21. </profile>
  22. </profiles>

2.效果:

需求:浏览发送/hello请求,响应 Hello,Spring Boot 2

3.引入SpringBoot的依赖

在第一次引入的时候会很慢,需要耐心等待:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.4.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. </dependencies>

4.创建主程序:

  1. package com.lsy.boot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @Description:主程序类
  6. * @Author:lsy
  7. * @Date:
  8. */
  9. //@SpringBootApplication:表示这是一个Springboot的应用
  10. @SpringBootApplication
  11. public class MainApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(MainApplication.class,args);
  14. }
  15. }

5.编写业务

  1. package com.lsy.boot.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * @Description:
  6. *
  7. * @RestController 中就包含了ResponseBody和Controller
  8. *
  9. * @Author:lsy
  10. * @Date:
  11. */
  12. @RestController
  13. public class HelloController {
  14. @GetMapping("/hello")
  15. public String handle01(){
  16. return "Hello,SpringBoot 2!";
  17. }
  18. }

6.测试

直接运行主程序中的main方法:

可以看到控制台的信息:

表示运行成功

 

7.一些相关的配置:

可以在application.properties中设置一些相关的配置:在SpringBoot的官方文档中可以找到

https://docs.spring.io/spring-boot/docs/2.4.4/reference/html/appendix-application-properties.html#common-application-properties

例如可以修改服务器的端口号:在application.properties写入以下语句就可以了

server.port=8888

8.SpringBoot的自动依赖配置:

父项目做依赖管理:

  1. 依赖管理
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.3.4.RELEASE</version>
  6. </parent>
  7. 他的父项目
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-dependencies</artifactId>
  11. <version>2.3.4.RELEASE</version>
  12. </parent>
  13. 几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制

在开发时导入starter场景启动器。就会自动导入依赖包:

  1. 1、见到很多 spring-boot-starter-* : *就某种场景,也就是某种引用所需要的的场景启动器
  2. 2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
  3. 3、SpringBoot所有支持的场景
  4. https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
  5. 4、见到的 *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。也就是自定义的场景启动器
  6. 5、所有场景启动器最底层的依赖
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter</artifactId>
  10. <version>2.3.4.RELEASE</version>
  11. <scope>compile</scope>
  12. </dependency>

无需关注版本号,自动版本仲裁

  1. 1、引入依赖默认都可以不写版本
  2. 2、引入非版本仲裁的jar,要写版本号。

可以修改默认的版本号:

  1. 1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
  2. 2、在当前项目里面重写配置
  3. <properties>
  4. <mysql.version>5.1.43</mysql.version>
  5. </properties>

 

9.自动配置

自动配好Tomcat

  • 引入Tomcat依赖。
  • 配置Tomcat
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-tomcat</artifactId>
  4. <version>2.3.4.RELEASE</version>
  5. <scope>compile</scope>
  6. </dependency>

自动配好SpringMVC

  • 引入SpringMVC全套组件
  • 自动配好SpringMVC常用组件(功能)

自动配好Web常见功能,如:字符编码问题

     SpringBoot帮我们配置好了所有web开发的常见场景

默认的包结构

  • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
  • 无需以前的包扫描配置
  • 想要改变扫描路径,@SpringBootApplication(scanBasePackages="com.atguigu")
  • 或者@ComponentScan 指定扫描路径
  1. @SpringBootApplication
  2. 等同于
  3. @SpringBootConfiguration
  4. @EnableAutoConfiguration
  5. @ComponentScan("com.atguigu.boot")

各种配置拥有默认值

  • 默认配置最终都是映射到某个类上,如:MultipartProperties
  • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
  • 按需加载所有自动配置项

非常多的starter

  • 引入了哪些场景这个场景的自动配置才会开启
  • SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/945818
推荐阅读
相关标签
  

闽ICP备14008679号