diff --git a/README.md b/README.md index c8d1090..05c55cf 100644 --- a/README.md +++ b/README.md @@ -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 的名称 diff --git a/docs/.vitepress/configs/nav.js b/docs/.vitepress/configs/nav.js index adbb030..e573d98 100644 --- a/docs/.vitepress/configs/nav.js +++ b/docs/.vitepress/configs/nav.js @@ -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" }, ], }, { diff --git a/docs/java/framework/spring/spel/spel.md b/docs/java/framework/spring/spel/spel.md new file mode 100644 index 0000000..e69de29 diff --git "a/docs/java/framework/springboot/\351\205\215\347\275\256\346\226\207\344\273\266.md" "b/docs/java/framework/springboot/\351\205\215\347\275\256\346\226\207\344\273\266.md" new file mode 100644 index 0000000..46260b2 --- /dev/null +++ "b/docs/java/framework/springboot/\351\205\215\347\275\256\346\226\207\344\273\266.md" @@ -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` 指定的配置文件中的值。 diff --git a/docs/java/utils/guava/utilities/stopWatch.md b/docs/java/utils/guava/utilities/stopWatch.md new file mode 100644 index 0000000..2a0ac49 --- /dev/null +++ b/docs/java/utils/guava/utilities/stopWatch.md @@ -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 +``` diff --git a/docs/outline/java/guava.md b/docs/outline/java/guava.md new file mode 100644 index 0000000..aff5c18 --- /dev/null +++ b/docs/outline/java/guava.md @@ -0,0 +1,5 @@ +# guava + +## Utilities + +- [stopWatch](/java/utils/guava/utilities/stopWatch.md) diff --git a/docs/outline/javaFramework.md b/docs/outline/javaFramework.md index 656af41..bde6a52 100644 --- a/docs/outline/javaFramework.md +++ b/docs/outline/javaFramework.md @@ -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) \ No newline at end of file + +- [index](../java/framework/springSecurity/index.md)