SpringBoot入门

SpringBoot入门

SpringBoot项目启动时,控制台输出哪些内容?

  1. 输出的第一行是JDK的路径
    在这里插入图片描述
  2. 还输出了项目启动的端口号,JDK版本、tomcat版本
  3. 最后一行是Application启动成功的日志

第一个Demo项目构建

  1. 参照官网:https://spring.io/quickstart
    也可以通过idea插件Spring initializr生成项目
    注意:SpringBoot 注解 @SpringBootApplication(启动类注解) 默认扫描当前类的同包以及子包下的类。
    不符合这个要求可能发生404,请求不到。
    如图结构:如果demo下有个controller是访问不到的。
    在这里插入图片描述
  2. 使用Junit,SpringBootTest进行单元测试。

Junit的使用(Junit 5版本)

@BeforeEach	在每个测试方法之前执行。注解在【非静态方法】上。
@AfterEach	在每个测试方法之后执行。注解在【非静态方法】上。
@BeforeAll	在当前类中的所有测试方法之前执行。注解在【静态方法】上。
@AfterAll		在当前类中的所有测试方法之后执行。注解在【静态方法】上。
@Test			加到测试方法上,标识测试方法。
@RunWith		加到测试类上,指定通过什么运行。如@RunWith(SpringRunner.class),
				在Spring容器下运行。

SpringBootTest的使用(常用注解)

@SpringBootTest		加载测试类上,是SpringBoot的一个用于测试的注解,通过					SpringApplication在测试中创建ApplicationContext。
			通过指明启动类,可以解决Unable to find a @SpringBootConfiguration, 
			you need to use @ContextConfiguration or @SpringBootTest(classes=...) 			with your test的异常问题。
@AutoConfigureMockMvc	是用于自动配置MockMvc,MockMvc可以实现模拟Http请求。

Demo如下:

package com.example.demo;

import com.flash.DemoApplication;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@AutoConfigureMockMvc
class DemoApplicationTests {

//	自动注入mockMvc对象
	@Autowired
	private MockMvc mvc;

	@BeforeEach
	void before(){
		System.out.println("===================测试开始==================");
	}

	@AfterEach
	void After(){
		System.out.println("===================测试结束==================");
	}

	@Test
	void test01() {
		System.out.println("===================测试中1==================");

	}

	@Test
	void test02() {
		System.out.println("===================测试中2==================");

	}

	@Test
	void test03() throws Exception {
		MvcResult result = mvc.perform(MockMvcRequestBuilders.get("/hello")
				.param("name","鲁智深"))
				.andReturn();
		System.out.println(result.getResponse().getContentAsString());
	}

}

总结:

  1. Junit可以单元测试,比如测试service业务层,测试mapper持久层都可以。
  2. 如果要测试完整的后台rest api服务,需要使用MockMvc去发起http请求。

配置文件-application.properties

  1. 配置数据库连接、日志相关配置
# mysql数据库连接参数
spring.datasource.url=jdbc:mysql://localhost:3306/dev
spring.datasource.username=root
spring.datasource.password=123456
# 可省略 mysql8 (com.mysql.cj.jdbc.Driver)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
server.port=8080
  1. 自定义配置\读取
配置
# 自定义测试属性
com.flash.test.name=鲁智深
com.flash.test.action=倒拔垂杨柳
# 参数替换
com.flash.test.desc=${com.flash.test.name}正在${com.flash.test.action}
读取,通过@Value注解
	@Value("${com.flash.test.name}")
    private String testName;
    @Value("${com.flash.test.action}")
    private String testAction;
    @Value("${com.flash.test.desc}")
    private String testDesc;
  1. 多环境多配置文件

配置文件满足application-{profile}.properties的格式,其中{profile}对应环境标识
比如:
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
在application.properties文件中通过spring.profiles.active属性设置对应{profile}值,控制哪个具体的
配置文件会被加载。如spring.profiles.active=test就会加载application-test.properties配置文件内容

  1. yml

除了.properties文件外,SpringBoot也支持.yml格式的配置文件。
示例:

spring:
  profiles:
    active: prod
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: root

其他

java -jar xxx.jar --server.port=8888,通过使用–server.port属性来设置xxx.jar应用的端口为8888。
等价于在application.properties中添加属性server.port=8888
禁用命令,提高系统安全性:SpringApplication.setAddCommandLineProperties(false)

Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了一种简化的方法来配置和部署应用程序,使开发人员能够更快地开发和运行应用程序。 Spring Boot Actuator是Spring Boot的一个组件,它可以帮助我们监控和管理Spring Boot应用程序,包括健康检查、审计、统计和HTTP追踪等功能。要使用Spring Boot Actuator,只需引入相应的起步依赖,并在应用程序的入口点类上添加@SpringBootApplication注解即可。在该类中,使用@SpringBootApplication注解相当于同时添加了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,它标识了当前应用程序是一个Spring Boot应用程序。要启动Spring Boot应用程序,只需在主启动类中编写main函数,通过调用SpringApplication.run(Application.class, args)方法来启动应用程序。在开发过程中,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot入门](https://blog.youkuaiyun.com/weixin_45905210/article/details/121712027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring boot 入门](https://blog.youkuaiyun.com/zhshx19900318/article/details/129476812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值