springboot的学习(三):开发相关

简介

一些开发测试时用到的技术。

springboot

热部署

修改了代码,服务器不需要重启可以直接看到新的修改的效果。仅仅加载当前开发者自定义开发的资源,不加载jar资源。
在pom.xml配置文件中添加:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <version>2.3.12.RELEASE</version>
            </dependency>

修改了内容之后,点击build project即可。
在这里插入图片描述

设置自动热部署

可以设置成不用点build project,项目自动的进行构建。
在这里插入图片描述
设置在项目运行中也可以自动构建,在idea上ctrl+alt+shift+/
在这里插入图片描述
在这里插入图片描述

热部署作用范围

可以设置一些目录的修改是不会触发热部署的。在yml配置文件中:

devlops:
  restart:
    exclude: 路径
关闭热部署
devlops:
  restart:
    enabled: false

第三方bean绑定属性

使用@ConfigurationProperties(prefix = “yml中的属性”)注解为第三方的bean绑定属性。
例子:
给datasource对象设置driverClassName
在配置文件中:

datasource:
  driverClassName: com.mysql.jdbc.Driver

在java类中:

@Bean
@ConfigurationProperties(prefix = "datasource")
public DruidDataSource datasource(){
DruidDataSource ds = new DruidDataSource ();
return ds;
}

为bean绑定属性

在配置文件application.xml中:

servers:
  ipAddress: 192.168.0.1
  port:8888
  timeout: -1

在pojo类中

@Component
@Data
@ConfigurationProperties(prefix="service")
public Class ServletConfig{
private String ipAddress;
private int port;
private long timeout;
}

注意:@ConfigurationProperties是松散绑定的

@EnableConfigurationProperties

可以将使用@ConfigurationProperties注解的类加入到spring容器中,为了方便统一管理,可以更便捷的看到哪些类使用了ConfigurationProperties。这个注解作用在启动类上,使用了这个注解,在实体类上就不能使用@component修饰了。

bean属性校验

在pom.xml文件中导入jsr303规范坐标和hibernate校验框架坐标:

            <dependency>
            	<groupId>javax.validation</groupId>
          	    <artifactId>validation-api</artifactId>
            </dependency>
            <dependency>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
            </dependency>

在bean中,@Validated开启校验,属性上面可以添加各种注解来校验,如果不符合规范,会提示注解中的message信息。

@Validated
public class User {
    private String name;
	@Max(value= 35,message="最大值不超过4")
    private Integer age;
    }

yaml配置文件数据转换

在配置文件yaml中,数字是支持2进制,8进制,16进制的

datasource:
  driverClassName: com.mysql.jdbc.Driver
  password: 0123

在测试类中使用@value读取

    @Value("${datasource.datatest}")
    private String datatest;
    @Test
void test01(){
    System.out.println(datatest);
}

在这里插入图片描述
springboot是把0123当成了八进制的数字了
可以在yaml中添加上双引号当成字符串接受。

datasource:
  driverClassName: com.mysql.jdbc.Driver
  datatest: "0123"

在这里插入图片描述

测试

临时属性

在测试类中,可以给测试类加上只在这个类中生效的属性配置,这个临时配置属性可以覆盖掉yaml中设置的属性。
yaml中有这个配置

datasource:
  driverClassName: com.mysql.jdbc.Driver
  datatest: "0123"

使用@SpringBootTest(properties = {“datasource.datatest=98765”})设置一个临时属性datasource.datatest

package com.christ.demo.test;

import com.christ.demo.DemoApplication;
import com.christ.demo.demos.service.test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(properties = {"datasource.datatest=98765"})
class DemoApplicationTests {
    @Value("${datasource.datatest}")
    private String datatest;
    @Test
void test01(){
    System.out.println(datatest);
}
}

运行结果:
在这里插入图片描述
也可以使用args属性设置,如果两个都有设置,以args为准

package com.christ.demo.test;

import com.christ.demo.DemoApplication;
import com.christ.demo.demos.service.test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(properties = {"datasource.datatest=98765"},args={"--datasource.datatest=7654321"})
class DemoApplicationTests {

    @Value("${datasource.datatest}")
    private String datatest;
    @Test
void test01(){
    System.out.println(datatest);
}
}

在这里插入图片描述

加载测试专用配置bean

可以使用@Import(类名.class)给测试类注入bean,就可以在测试类中使用@Autowired注解自动装配使用类了。

@SpringBootTest
@Import(datasource.class)
class DemoApplicationTests {
    @Autowired
    private datasource datasource;
    @Test
	void test01(){
	datasource.say();}
}

测试controller层

要测试controller,需要有web环境,可以在测试类中的@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvc开启虚拟mvc调用

@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)@Import(datasource.class)
@AutoConfigureMockMvc
//开启虚拟mvc调用
class DemoApplicationTests {
    @Test
void test01(@Autowired MockMvc mvc) throws Exception {
    //创建虚拟请求,
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");
        //执行请求
        ResultActions actions = mvc.perform(builder);
        //然后可以定个各种响应的预期值,做断言判断,各个组成部分信息是否匹配
         StatusResultMatchers status = MockMvcResultMatchers.status();
         ResultMatcher ok = status.isOk();
         actions.andExpect(ok);
		
		 HeaderResultMatchers header = MockMvcResultMatchers.header();
         ResultMatcher contentType = header.string("Context-Type", "application/json");
         actions.andExpect(contentType); 

        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher json = content.json("{\"name\":\"christ\"}");
        actions.andExpect(json);
    }
}

在这里插入图片描述

测试使用事务回滚

不希望测试的时候产生的数据存储到数据库中,即不产生垃圾数据,可以在测试类中使用@Transaction注解。

数据层解决方案

数据源:druiddatasource
持久化技术:mybatis-plus或mybatis
数据库:mysql

如果配置文件中没有指定数据源,springboot默认的数据源是hikari,或者Tomcat中的datasource。

监控

  • 监控服务状态是否宕机
  • 日志
  • 各种服务运行指标,如内存,虚拟机,线程,请求
  • 管理服务,下线上线等
  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值