Skip to content

Commit

Permalink
stopWatch.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspercliff committed Aug 20, 2024
1 parent a05f388 commit 1e85570
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ todo
- jmap
- spring boot starter
- guava stopwatch 事件总线
- logback 日志
- spring batch
- [Profiles :: Spring Boot](https://docs.spring.io/spring-boot/reference/features/profiles.html)
- BeanFactory and ApplicationContext [The BeanFactory API :: Spring Framework](https://docs.spring.io/spring-framework/reference/core/beans/beanfactory.html)
- bean 没有指定名称 默认情况下,Spring 将使用无下划线的小写驼峰命名法(camelCase)生成 Bean 的名称
1 change: 1 addition & 0 deletions docs/.vitepress/configs/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const nav = [
{ text: "jvm", link: "/outline/jvm.md" },
{ text: "framework", link: "/outline/javaFramework.md" },
{ text: "设计模式", link: "/outline/designPattern.md" },
{ text: "guava", link: "/outline/java/guava.md" },
],
},
{
Expand Down
Empty file.
48 changes: 48 additions & 0 deletions docs/java/framework/springboot/配置文件.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 配置文件



`spring.profiles.include`允许在应用启动时自动包含额外的配置文件。这在处理不同环境的共享配置或者模块化配置时特别有用。

### 如何使用 `spring.profiles.include`

1. **定义配置文件**

-`src/main/resources` 目录下创建对应的配置文件。例如,如果你有一个名为 `common` 的配置集,可以创建一个名为 `application-common.properties``application-common.yml` 的文件。
2. **启用自动包含**

- 在你的主配置文件(通常是 `application.properties``application.yml`)中添加如下属性:
```properties
spring.profiles.include=common
```
- 这条配置告诉 Spring Boot 在任何其他配置文件加载之前先加载 `application-common.*` 文件中的配置。
3. **使用特定环境的配置**:

- 如果你有多个环境(如开发、测试和生产),你可以为每个环境定义特定的配置文件(如 `application-dev.properties`),并使用 `spring.profiles.active` 属性来激活相应的环境配置:
```properties
spring.profiles.active=dev
```

### 示例

假设你有两个配置文件:`application-common.properties` 和 `application-dev.properties`。

- `application-common.properties` 包含所有环境共有的设置。
- `application-dev.properties` 包含开发环境特有的设置。

在 `application.properties` 中添加以下内容:

```properties
spring.profiles.include=common
spring.profiles.active=dev
```

这样,无论何时你以开发环境运行应用程序,都会同时加载 `application-common.properties``application-dev.properties`

### 注意事项

- 如果在多个配置文件中有相同的属性定义,那么后加载的配置文件中的值会覆盖先前的值。
- 可以使用逗号分隔来指定多个要包含的配置文件名称,例如 `spring.profiles.include=common,logging`

* `spring.profiles.include` 指定的配置文件总是先于 `spring.profiles.active` 指定的配置文件被加载。
* 如果两个配置文件中有相同的属性,那么 `spring.profiles.active` 指定的配置文件中的值将覆盖 `spring.profiles.include` 指定的配置文件中的值。
126 changes: 126 additions & 0 deletions docs/java/utils/guava/utilities/stopWatch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
`Stopwatch` 是 Guava 库中提供的一种用于测量代码执行时间的工具类。它可以帮助开发者轻松地记录和计算代码片段的执行时间。

### 使用方法

1. **创建 Stopwatch 实例**

```java
import com.google.common.base.Stopwatch;

Stopwatch stopwatch = Stopwatch.createStarted();
```
2. **开始计时**
如果需要手动控制开始计时点,可以先创建未启动的 `Stopwatch`,然后调用 `start()` 方法来开始计时。

```java
Stopwatch stopwatch = Stopwatch.createUnstarted();
stopwatch.start();
```
3. **停止计时**
调用 `stop()` 方法可以停止计时。

```java
stopwatch.stop();
```
4. **获取经过的时间**
可以通过多种方式获取经过的时间,包括但不限于 `elapsed()` 方法,它可以返回从开始到现在的总时间(即使已经停止)。

```java
long elapsedTimeMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS);
```
5. **重置计时器**
如果想重新开始计时,可以调用 `reset()` 方法来重置 `Stopwatch`

```java
stopwatch.reset();
```
6. **持续计时**
即使在停止后,再次启动 `Stopwatch` 会继续之前的计时,而不是重新开始。

```java
stopwatch.start(); // 继续计时
```
7. **使用上下文管理**
在 Java 8 及以上版本中,可以使用 try-with-resources 语法来自动管理 `Stopwatch` 的生命周期(虽然 `Stopwatch` 并不实现 `AutoCloseable` 接口,但可以通过自定义实现达到类似效果)。

```java
try (Stopwatch stopwatch = new Stopwatch()) {
// 执行代码
stopwatch.start();
// ...
// 当退出try块时,stopwatch将被自动停止
}
```

### 示例

下面是一个简单的示例,演示如何使用 `Stopwatch` 来测量一段代码的执行时间:

```java
import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;

public class StopwatchExample {
public static void main(String[] args) {
Stopwatch stopwatch = Stopwatch.createStarted();

// 假设这是要测量的部分
for (int i = 0; i < 1000000; i++) {
Math.random(); // 做一些计算
}

stopwatch.stop();
System.out.println("Total time elapsed: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
}
}
```

### 注意事项

- `Stopwatch` 不是线程安全的,如果你在一个多线程环境中使用它,你需要确保在每个线程中都有独立的 `Stopwatch` 实例或者适当同步它的访问。

```java
package com.jasper;

import com.google.common.base.Stopwatch;

import java.util.concurrent.TimeUnit;

/**
* @Author jasper
* @Date 2024-08-20
* @version 1.0
* @description stopwatch learn
*/
public class StopWatchDemo {
public static void main(String[] args) {
Stopwatch stopwatch = Stopwatch.createStarted();
System.out.println("stopwatch = " + stopwatch);
for (int i = 0; i < 10000; i++) {
Math.random();
}
System.out.println("stopwatch.isRunning() = " + stopwatch.isRunning());
stopwatch.stop();
System.out.println("after stop stopwatch.isRunning() = " + stopwatch.isRunning());
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
System.out.println("elapsed = " + elapsed+"ms");

System.out.println("elapsed = " + elapsed);

System.out.println("stopwatch = " + stopwatch);
Stopwatch reset = stopwatch.reset();
System.out.println("reset = " + reset);
}
}
```

```java
stopwatch = 81.40 μs
stopwatch.isRunning() = true
after stop stopwatch.isRunning() = false
elapsed = 57ms
elapsed = 57
stopwatch = 57.52 ms
reset = 0.000 ns
```
5 changes: 5 additions & 0 deletions docs/outline/java/guava.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# guava

## Utilities

- [stopWatch](/java/utils/guava/utilities/stopWatch.md)
12 changes: 11 additions & 1 deletion docs/outline/javaFramework.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# java framework

## spring

### core

- [ioc](../java/framework/spring/core/ioc.md)
- [aop](../java/framework/spring/core/aop.md)
- [annotation](/java/framework/spring/annotation)

### data access

- [transaction](../java/framework/spring/dataAccess/transaction.md)

## springboot

### 集成

- [springboot集成sqlite](../java/framework/springboot/sqlite.md)
- [springboot集成freemarker](../java/framework/springboot/freemarker.md)

## springSecurity
- [index](../java/framework/springSecurity/index.md)

- [index](../java/framework/springSecurity/index.md)

0 comments on commit 1e85570

Please sign in to comment.