SpringBoot.01.SpringBoot概述及基本环境搭建

x33g5p2x  于2021-09-19 转载在 Spring  
字(7.6k)|赞(0)|评价(0)|浏览(581)

SpringBoot概述

引言

SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的 初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不 再需要定义样板化的配置。通过这种方式,SpringBoot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
本教程使用的SpringBoot版本为2.5.0(自2.4.0开始,版本号后面不再有RELEASE

SpringBoot优势

  1. 创建完整的独立的Spring应用程序 :在SSM框架中存在父子容器的概念,在SpringBoot中只有一个容器
  2. 嵌入的Tomcat,无需部署WAR文件: SpringBoot内嵌tomcat,应用跑在内嵌服务器
  3. 简化Maven配置,自动配置Spring与Springmvc,不需要配置XML :SpringBoot只需要一个依赖就能引入原先SSM中需要N多依赖才能引入的Spring家族依赖

SpringBoot约定

springboot项目中必须在src/main/resources中放入application.yml(.properties)核心配置文件,名字必须为application

springboot项目中必须在src/main/java中所有子包之外构建全局入口类xxApplication.java,一个springboot项目只能有一个入口类

环境搭建

环境要求

  1. jdk 1.8.+
  2. maven 3.6.3
  3. SpringBoot 2.5.0

准备工作

1.新建项目空间

打开IDEA,选择File->New->Project,我们选择Empty Project。在下图中填写项目名–springboot-parent、选择项目位置,然后点击Finish。如下图所示:

2.配置Maven

新的项目创建完毕后一定要先检查Maven的位置是不是自己本机的maven。最后点击Appky->OK即可

3.配置Log Support

Other Settings中找到Log Support,修改其Frameworkslf4j。如下图所示:

创建项目的两种方式

maven方式搭建
1.新建Module

在IDEA中依次选择File->New->Module,我们选择Maven。按照下图所示填写相应信息,最后点击Finish

2.pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.christy</groupId>
  5. <artifactId>springboot-01-maven</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.5.0</version>
  11. </parent>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. </dependencies>
  22. </project>
3.application.yml
  1. server:
  2. port: 8801 # springboot默认端口号是8080,可以通过此方式修改默认端口号
  3. servlet:
  4. context-path: /springboot # springboot默认是不带项目名的,可以通过此方式配置项目名,但是项目名前面必须跟'/'
4.SpringBoot01MavenApplication.java
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. /** * @Author Christy * @Date 2021/9/1 9:56 * * @SpringBootApplication 该注解作用于入口类,有且只有一个 * 该注解是一个组合注解,有以下7个注解修饰 * @Target({ElementType.TYPE}) 该注解表示被修饰注解的作用范围 * @Retention(RetentionPolicy.RUNTIME) 该注解表示被修饰注解的作用时机 * @Documented * @Inherited * 以上4个注解称之为元注解(修饰注解的注解) * * @SpringBootConfiguration 标识这是一个springboot的配置类,默认自定配置Spring环境 * @EnableAutoConfiguration 自动与项目中集成的第三方技术进行集成。比如redis、es等 * @ComponentScan 扫描入口类所在子包以及子包后代包中注解 **/
  4. @SpringBootApplication
  5. public class SpringBoot01MavenApplication {
  6. public static void main(String[] args){
  7. // args: 该参数可以在启动时指定jvm参数覆盖默认配置
  8. SpringApplication.run(SpringBoot01MavenApplication.class, args);
  9. }
  10. }
5.TestController.java
  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /** * @Author Christy * @Date 2021/9/1 10:12 * * @RestController: 表示该类是一个Controller用来接收HTTP请求 * 该注解等价于@Controller + @ResponseBody * @ResponseBody 该注解用于将Controller的方法返回的对象, * 通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。 * 使用此注解此次请求将不再走视图处理器,而是直接将此响应结果写入到输入流中,其效果等同于使用response对象输出指定格式的数据 * @Component 通用的对象创建注解.在spring工厂中创建当前对象(通俗的讲就是将当前对象交由Spring管理) * @Controller 用来创建控制器对象 * @Service 用来创建业务层对象 * @Repository` 用来创建DAO层对象 **/
  6. @RestController
  7. @RequestMapping("test")
  8. public class TestController {
  9. private static final Logger log = LoggerFactory.getLogger(TestController.class);
  10. @RequestMapping("hello")
  11. public String sayHello(){
  12. log.info("Hello SpringBoot!");
  13. return "Hello SpringBoot!";
  14. }
  15. }
6.测试

我们直接运行main函数,启动当前项目。如下图所示:

由于我们配置了context-path,所以我们的访问路径应该是http://localhost:8801/springboot/test/hello。如下图所示:

Spring initializr方式
1.新建Module

在IDEA中依次选择File->New->Module,我们选择Spring initializr。按照下图所示填写相应信息,最后点击Next

Dependencies中选择Spring Web,最后点击Finish。如下图所示:

2.pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>2.5.0</version>
  7. <relativePath/>
  8. </parent>
  9. <groupId>com.christy</groupId>
  10. <artifactId>springboot-02-initializr</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <name>springboot-02-initializr</name>
  13. <description>Demo project for Spring Boot</description>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. </dependency>
  27. </dependencies>
  28. <build>
  29. <plugins>
  30. <!-- maven的运行插件,默认Maven方式创建的项目(空架构)是没有该插件的需要手动添加 如果没有该插件的话,则打包成jar包后通过java -jar 运行是会报无法加载到主类的错误 -->
  31. <plugin>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-maven-plugin</artifactId>
  34. </plugin>
  35. </plugins>
  36. </build>
  37. </project>
3.application.yml

Spring Initializr方式创建的项目配置文件默认为.properties,这里我们手动修改为.yml

这里我们不设置context-path

  1. server:
  2. port: 8802
4.Springboot02InitializrApplication.java
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class Springboot02InitializrApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Springboot02InitializrApplication.class, args);
  7. }
  8. }
5.TestController.java
  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /** * @Author Christy * @Date 2021/9/1 11:06 **/
  6. @RestController
  7. @RequestMapping("test")
  8. public class TestController {
  9. private static final Logger log = LoggerFactory.getLogger(TestController.class);
  10. @RequestMapping("hello")
  11. public String sayHello(){
  12. log.info("Hello SpringBoot!");
  13. return "Hello SpringBoot!";
  14. }
  15. }
6.测试

直接启动项目,浏览器访问http://localhost:8802/test/hello。如下图所示:

配置文件拆分

在实际开发过程中生产环境和测试环境有可能是不一样的,因此将生产中的配置和测试中的配置拆分开是非常必要的。在springboot中也提供了配置文件拆分的方式。这里以开发与生产中端口号不一致为例,在Springboot-02-Initializr中举例说明

1.application-dev.yml

  1. # 开发环境端口号是8802
  2. server:
  3. port: 8802

2.application-prod.yml

  1. # 生产环境端口号是8803
  2. server:
  3. port: 8803

3.application.yml

  1. #在主配置中指定那个环境生效配置
  2. spring:
  3. profiles:
  4. active: dev #指定那个环境配置生效 dev为环境配置文件的简名

4.TestController

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /** * @Author Christy * @Date 2021/9/1 11:06 **/
  7. @RestController
  8. @RequestMapping("test")
  9. public class TestController {
  10. private static final Logger log = LoggerFactory.getLogger(TestController.class);
  11. @Value("${server.port}")
  12. private Integer port;
  13. @RequestMapping("hello")
  14. public String sayHello(){
  15. log.info("Hello SpringBoot!");
  16. return "Hello SpringBoot! current port is " + port;
  17. }
  18. }

5.测试

启动项目。浏览器直接访问http://localhost:8802/test/hello。由于我们当前使用的dev的配置文件,所以访问端口号8802是通的,8803则无法访问。如下图所示:

相关文章