springboot基础入门2(profile应用)

Profile应用

一、何为Profile

在开发springboot应用时。通常一套程序会被安装在不同环境中(比如开发,测试,生产),其中数据库地址,服务器端口等等配置都不同,如果每次打包是,都要修改配置文件,就会非常麻烦,profile可以进行动态配置切换

二、profile配置方式

1.多profile文件方式

applicatio.yml

bash 复制代码
---

server:
  port: 8081

spring:
  profiles: dev
---

server:
  port: 8082

spring:
  profiles: test
---
server:
  port: 8083

spring:
  profiles: pro

每一个框内都是一组配置

激活使用

bash 复制代码
---
spring:
  profiles:
    active: dev

2.yml多文档方式

三、加载顺序

1. file:./config/: 当前项目下的/config目录下

2. file:./ :当前项目的根目录

3. classpath:/config/:classpath的/config目录

4. classpath:/ : classpath的根目录

高优先级属性会生效,但是每个文件都会读取,只是生效与否

四、profile激活方式

1. 配置文件

2. 虚拟机参数

部署方法

3. 命令行参数

五、测试类编写

1. 不同目录下的编写

若目录com.itheima.springboottest.UserService方法

bash 复制代码
package com.itheima.springboottest;

import org.springframework.stereotype.Service;

@Service
public class UserService {


    public void add() {
        System.out.println("add...");
    }
}

测试类目录为com.itheima.test.UserServiceTest 与上面不一致,一个是applicationtest一个是test

bash 复制代码
package com.itheima.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * userService的测试类
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)
# 不同目录,这里的classes不可以省略
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testAdd() {
        userService.add();
    }
}
  1. RunWith通常是一个用于测试的编程概念,特别是在框架如JUnit(Java Unit Testing Framework)或.NET的Moq、NUnit等中。RunWith的作用是用来标记或装饰测试方法,告诉测试运行器如何执行这个特定的测试。例如,在JUnit中,@RunWith(Suite.class)表明该测试类应该作为一组测试用例的集合(suite)来运行,而不是独立的测试。

  2. 在Java中,@ RunWith注解通常放在测试类的定义上方,这样测试框架就能识别并按照指定的方式运行测试方法。如果你看到RunWith并且是编程相关的上下文,那可能是在讨论单元测试或行为驱动开发(BDD)中的测试组织方式。

  3. @Autowired 是Spring框架中的一个注解,用于依赖注入(Dependency Injection, DI)机制。它是一个懒加载注解,用于自动装配bean到其他bean中,简化了组件之间的依赖管理。当你在一个字段、方法参数或构造器上使用 @Autowired,Spring容器会尝试找到并注入合适的bean实例,满足该字段或方法的需求。

  4. @Autowired 具体使用时,例如在控制器、服务类或DAO接口等地方,你不再需要显式地创建和管理这些对象,Spring会在运行时自动完成这个过程。这有助于降低代码的耦合度,使得组件更加松耦合,提高代码的可测试性和可用性

2. 同目录下

bash 复制代码
package com.itheima.springboottest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * userService的测试类
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testAdd() {
        userService.add();
    }
}

六.导入redis

相关推荐
豌豆花下猫5 小时前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
ST.J5 小时前
swing笔记
java·笔记
菩提树下的凡夫5 小时前
瑞芯微RV1126目标识别算法Yolov8的部署应用
java·算法·yolo
尚学教辅学习资料5 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
爱隐身的官人5 小时前
新后端漏洞(上)- Java RMI Registry反序列化漏洞
java·反序列化漏洞
叫我阿柒啊5 小时前
从Java全栈到前端框架:一次真实的面试对话与技术解析
java·javascript·typescript·vue·springboot·react·前端开发
晚安里5 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
秋难降5 小时前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
爱隐身的官人6 小时前
新后端漏洞(上)- Aapache Tomcat AJP 文件包含漏洞(CVE-2020-1938)
java·tomcat·ajp
@CLoudbays_Martin116 小时前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php