diff --git a/CHANGELOG.md b/CHANGELOG.md index 246a0ab..e718034 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,43 @@ 本文件记录 mingli-utils 各版本的主要变更。 +## [5.7.2] - 2026-11-23 + +### 新增 + +- **spring**:新增 `@FastServiceScan` 注解及 `FastServiceScanRegistrar`、 + `FastServiceScannerProcessor`——扫描继承 `FastBaseIService` 或 `FastBaseIServiceNew3518` + 的具体类并注册为 Spring Bean;仅在启动类/配置类标注该注解时生效,扫描时机在所有 + `BeanDefinitionRegistryPostProcessor`(包括 `@MapperScan` 的 `MapperScannerConfigurer`)之后。 + 支持 `value` / `basePackages` / `basePackageClasses`,`value` 与 `basePackages` 通过 + `@AliasFor` 互为别名;未指定包时使用标注类所在包;已标注 `@Component` / `@Service` + 的类自动跳过,避免重复注册。 +- **test**:新增 `FastServiceScanRegistrarTest`(5 个用例)与 + `FastServiceScannerProcessorTest`(2 个用例,覆盖两个基类与某个基类缺失时的跳过逻辑)。 + +### 变更 + +- **spring**:`ApplicationDestroyedRunner`、`ApplicationReadyEventListener`、 + `ApplicationStartedRunner`、`ContextClosedEventListener` 由 Java 迁移为 Kotlin 实现, + 包名、属性及 `@AliasFor` 关系保持不变。 +- **spring**:`ApplicationDestroyedRunner` / `ApplicationStartedRunner` 补全 + `@Deprecated` + `ReplaceWith`,IDE 可一键替换为 `ContextClosedEventListener` / + `ApplicationReadyEventListener`;旧 Java 版本只有 `@Deprecated(since = ...)`。 +- **build**:新增 `VERSIONSTYPE`(`r` = RELEASES,`s` = SNAPSHOT)控制版本后缀; + 补充 Spring / MyBatis-Plus 测试依赖。 + +### 修复 + +- **spring**:`FastServiceScannerProcessor` 改为按类名探测两个服务基类, + 仅引入一套 MyBatis-Plus 时只跳过缺失的基类,不再因类字面量加载父类失败抛出 + `NoClassDefFoundError`。 + +### 注意 + +- 事件监听注解的 `defaultExecution` 属性仅在 Spring Framework 6.2+ 中提供; + 在 Spring 5.3 下解析这些组合注解会抛出 `AnnotationConfigurationException`,使用方需按实际 + Spring 版本选择。 + ## [5.7.0] - 2026-11-23 ### 新增 diff --git a/build.gradle.kts b/build.gradle.kts index 76696de..016adda 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils * CurrentFile build.gradle.kts - * LastUpdate 2026-11-23 10:24:07 + * LastUpdate 2026-11-23 15:17:59 * UpdateUser MingLiPro */ @@ -60,10 +60,12 @@ plugins { kotlin("plugin.spring") version "2.1.0" } val GROUPSID = providers.gradleProperty("GROUPSID").get() -val VERSIONS = providers.gradleProperty("VERSIONS").get() +val VERSION = providers.gradleProperty("VERSIONS").get() val ARTIFACTID = providers.gradleProperty("ARTIFACTID").get() val JDKVERSIONS = providers.gradleProperty("JDKVERSIONS").get().toInt() - +val VERSIONSTYPE = providers.gradleProperty("VERSIONSTYPE").get() +val VERSIONS = + VERSION + if (VERSIONSTYPE == "s") "-SNAPSHOT" else if (VERSIONSTYPE == "r") "" else error("VERSIONSTYPE is not supported in gradle.properties $VERSIONSTYPE") group = GROUPSID version = VERSIONS @@ -117,6 +119,11 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.7.1") testRuntimeOnly("org.junit.platform:junit-platform-launcher") testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2") + testImplementation(fileTree("libs")) + testImplementation("com.baomidou:mybatis-plus-core:3.5.15") + testImplementation("com.baomidou:mybatis-plus-extension:3.5.15") + testImplementation("com.baomidou:mybatis-plus-spring:3.5.9") + testImplementation("org.springframework:spring-context:5.3.39") testImplementation("org.redisson:redisson:4.8.0") testImplementation("com.mingliqiye.logger:logger-log4j2:1.0.9") testImplementation("com.google.code.gson:gson:2.13.2") diff --git a/gradle.properties b/gradle.properties index a5c78ba..9be4d85 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,10 +16,13 @@ # ProjectName mingli-utils # ModuleName mingli-utils # CurrentFile gradle.properties -# LastUpdate 2026-11-23 10:43:58 +# LastUpdate 2026-11-23 15:24:00 # UpdateUser MingLiPro # JDKVERSIONS=17 GROUPSID=com.mingliqiye.utils ARTIFACTID=mingli-utils -VERSIONS=5.7.0 +# r = RELEASES +# s = SNAPSHOT +VERSIONSTYPE=r +VERSIONS=5.7.2 diff --git a/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationDestroyedRunner.java b/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationDestroyedRunner.java deleted file mode 100644 index 6999dc8..0000000 --- a/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationDestroyedRunner.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile ApplicationDestroyedRunner.java - * LastUpdate 2026-11-18 08:21:37 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.springboot.annotation; - -import org.springframework.context.event.ContextClosedEvent; -import org.springframework.context.event.EventListener; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.AliasFor; -import org.springframework.core.annotation.Order; - -import java.lang.annotation.*; - -/** - * 应用销毁(上下文关闭)事件监听注解。 - * - *
标注在 Spring Bean 的方法上,当 Spring 应用上下文关闭 - * ({@link ContextClosedEvent} 发布)时自动调用该方法。
- * - * @deprecated 已弃用,请改用 {@link ContextClosedEventListener}(监听相同事件,命名更规范)。 - * @see ContextClosedEventListener - * @see EventListener - * @see ContextClosedEvent - */ -@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@EventListener(ContextClosedEvent.class) -@Order -@Deprecated(since = "use ApplicationReadyEventListener") -public @interface ApplicationDestroyedRunner { - /** - * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。 - * - * @return 条件表达式,默认为空字符串(无条件执行) - */ - @AliasFor(annotation = EventListener.class, attribute = "condition") - String condition() default ""; - - /** - * 当事件不是 {@link ContextClosedEvent} 类型时是否仍默认执行监听方法。 - * - * @return 是否默认执行,默认为 true - */ - @AliasFor(annotation = EventListener.class, attribute = "defaultExecution") - boolean defaultExecution() default true; - - /** - * 监听器的唯一标识,用于注册可指定 id 的监听器。 - * - * @return 监听器 id,默认为空字符串(自动生成) - */ - @AliasFor(annotation = EventListener.class, attribute = "id") - String id() default ""; - - /** - * 监听器的执行顺序,值越小优先级越高。 - * - * @return 顺序值,默认为 {@link Ordered#LOWEST_PRECEDENCE} - * @see Ordered#getOrder() - */ - @AliasFor(annotation = Order.class, attribute = "value") - int order() default Ordered.LOWEST_PRECEDENCE; -} diff --git a/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationReadyEventListener.java b/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationReadyEventListener.java deleted file mode 100644 index 43df7c6..0000000 --- a/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationReadyEventListener.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile ApplicationReadyEventListener.java - * LastUpdate 2026-11-18 08:21:57 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.springboot.annotation; - - -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.event.EventListener; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.AliasFor; -import org.springframework.core.annotation.Order; - -import java.lang.annotation.*; - - -/** - * 应用就绪事件监听注解。 - * - *标注在 Spring Bean 的方法上,当 Spring Boot 应用完成启动并就绪 - * ({@link ApplicationReadyEvent} 发布)时自动调用该方法, - * 等价于 {@code @EventListener(ApplicationReadyEvent.class)} 的组合简化写法。
- * - *使用示例:
- *{@code
- * @Component
- * public class InitTask {
- * @ApplicationReadyEventListener(order = 0)
- * public void onReady() {
- * // 应用就绪后执行的初始化逻辑
- * }
- * }
- * }
- *
- * @see EventListener
- * @see ApplicationReadyEvent
- */
-@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-@EventListener(ApplicationReadyEvent.class)
-@Order
-public @interface ApplicationReadyEventListener {
- /**
- * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。
- *
- * @return 条件表达式,默认为空字符串(无条件执行)
- */
- @AliasFor(annotation = EventListener.class, attribute = "condition")
- String condition() default "";
-
- /**
- * 当事件不是 {@link ApplicationReadyEvent} 类型时是否仍默认执行监听方法。
- *
- * @return 是否默认执行,默认为 true
- */
- @AliasFor(annotation = EventListener.class, attribute = "defaultExecution")
- boolean defaultExecution() default true;
-
- /**
- * 监听器的唯一标识,用于注册可指定 id 的监听器。
- *
- * @return 监听器 id,默认为空字符串(自动生成)
- */
- @AliasFor(annotation = EventListener.class, attribute = "id")
- String id() default "";
-
-
- /**
- * 监听器的执行顺序,值越小优先级越高。
- *
- * @return 顺序值,默认为 {@link Ordered#LOWEST_PRECEDENCE}
- * @see Ordered#getOrder()
- */
- @AliasFor(annotation = Order.class, attribute = "value")
- int order() default Ordered.LOWEST_PRECEDENCE;
-}
diff --git a/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationStartedRunner.java b/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationStartedRunner.java
deleted file mode 100644
index 437a6f2..0000000
--- a/src/main/java/com/mingliqiye/utils/springboot/annotation/ApplicationStartedRunner.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright 2026 mingliqiye
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * ProjectName mingli-utils
- * ModuleName mingli-utils.main
- * CurrentFile ApplicationStartedRunner.java
- * LastUpdate 2026-11-18 08:21:57
- * UpdateUser MingLiPro
- */
-
-package com.mingliqiye.utils.springboot.annotation;
-
-import org.springframework.boot.context.event.ApplicationReadyEvent;
-import org.springframework.context.event.EventListener;
-import org.springframework.core.Ordered;
-import org.springframework.core.annotation.AliasFor;
-import org.springframework.core.annotation.Order;
-
-import java.lang.annotation.*;
-
-/**
- * 应用启动完成事件监听注解。
- *
- * 标注在 Spring Bean 的方法上,当 Spring Boot 应用完成启动并就绪 - * ({@link ApplicationReadyEvent} 发布)时自动调用该方法。
- * - * @deprecated 已弃用,请改用 {@link ApplicationReadyEventListener}(功能完全一致,命名更规范)。 - * @see ApplicationReadyEventListener - * @see EventListener - * @see ApplicationReadyEvent - */ -@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@EventListener(ApplicationReadyEvent.class) -@Order -@Deprecated(since = "use ApplicationReadyEventListener") -public @interface ApplicationStartedRunner { - /** - * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。 - * - * @return 条件表达式,默认为空字符串(无条件执行) - */ - @AliasFor(annotation = EventListener.class, attribute = "condition") - String condition() default ""; - - /** - * 当事件不是 {@link ApplicationReadyEvent} 类型时是否仍默认执行监听方法。 - * - * @return 是否默认执行,默认为 true - */ - @AliasFor(annotation = EventListener.class, attribute = "defaultExecution") - boolean defaultExecution() default true; - - /** - * 监听器的唯一标识,用于注册可指定 id 的监听器。 - * - * @return 监听器 id,默认为空字符串(自动生成) - */ - @AliasFor(annotation = EventListener.class, attribute = "id") - String id() default ""; - - - /** - * 监听器的执行顺序,值越小优先级越高。 - * - * @return 顺序值,默认为 {@link Ordered#LOWEST_PRECEDENCE} - * @see Ordered#getOrder() - */ - @AliasFor(annotation = Order.class, attribute = "value") - int order() default Ordered.LOWEST_PRECEDENCE; -} diff --git a/src/main/java/com/mingliqiye/utils/springboot/annotation/ContextClosedEventListener.java b/src/main/java/com/mingliqiye/utils/springboot/annotation/ContextClosedEventListener.java deleted file mode 100644 index 08afd9c..0000000 --- a/src/main/java/com/mingliqiye/utils/springboot/annotation/ContextClosedEventListener.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2026 mingliqiye - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ProjectName mingli-utils - * ModuleName mingli-utils.main - * CurrentFile ContextClosedEventListener.java - * LastUpdate 2026-11-18 08:21:23 - * UpdateUser MingLiPro - */ - -package com.mingliqiye.utils.springboot.annotation; - -import org.springframework.context.event.ContextClosedEvent; -import org.springframework.context.event.EventListener; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.AliasFor; -import org.springframework.core.annotation.Order; - -import java.lang.annotation.*; - -/** - * 应用上下文关闭事件监听注解。 - * - *标注在 Spring Bean 的方法上,当 Spring 应用上下文关闭 - * ({@link ContextClosedEvent} 发布)时自动调用该方法, - * 等价于 {@code @EventListener(ContextClosedEvent.class)} 的组合简化写法, - * 适用于应用停机前的资源清理、状态保存等场景。
- * - *使用示例:
- *{@code
- * @Component
- * public class CleanupTask {
- * @ContextClosedEventListener
- * public void onClosed() {
- * // 上下文关闭前执行的清理逻辑
- * }
- * }
- * }
- *
- * @see EventListener
- * @see ContextClosedEvent
- */
-@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-@EventListener(ContextClosedEvent.class)
-@Order
-public @interface ContextClosedEventListener {
- /**
- * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。
- *
- * @return 条件表达式,默认为空字符串(无条件执行)
- */
- @AliasFor(annotation = EventListener.class, attribute = "condition")
- String condition() default "";
-
- /**
- * 当事件不是 {@link ContextClosedEvent} 类型时是否仍默认执行监听方法。
- *
- * @return 是否默认执行,默认为 true
- */
- @AliasFor(annotation = EventListener.class, attribute = "defaultExecution")
- boolean defaultExecution() default true;
-
- /**
- * 监听器的唯一标识,用于注册可指定 id 的监听器。
- *
- * @return 监听器 id,默认为空字符串(自动生成)
- */
- @AliasFor(annotation = EventListener.class, attribute = "id")
- String id() default "";
-
- /**
- * 监听器的执行顺序,值越小优先级越高。
- *
- * @return 顺序值,默认为 {@link Ordered#LOWEST_PRECEDENCE}
- * @see Ordered#getOrder()
- */
- @AliasFor(annotation = Order.class, attribute = "value")
- int order() default Ordered.LOWEST_PRECEDENCE;
-}
diff --git a/src/main/java/com/mingliqiye/utils/springboot/annotation/FastServiceScan.java b/src/main/java/com/mingliqiye/utils/springboot/annotation/FastServiceScan.java
new file mode 100644
index 0000000..5e2cf11
--- /dev/null
+++ b/src/main/java/com/mingliqiye/utils/springboot/annotation/FastServiceScan.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2026 mingliqiye
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ProjectName mingli-utils
+ * ModuleName mingli-utils.main
+ * CurrentFile FastServiceScan.java
+ * LastUpdate 2026-11-23 14:44:48
+ * UpdateUser MingLiPro
+ */
+
+package com.mingliqiye.utils.springboot.annotation;
+
+import com.mingliqiye.utils.springboot.autoconfigure.FastServiceScanRegistrar;
+import org.springframework.context.annotation.Import;
+import org.springframework.core.annotation.AliasFor;
+
+import java.lang.annotation.*;
+
+/**
+ * 扫描继承自 {@code com.mingliqiye.utils.mybatisplus.FastBaseIService} 或
+ * {@code com.mingliqiye.utils.mybatisplus.FastBaseIServiceNew3518} 的具体类并注册为 Spring Bean。
+ * 两个基类分别对应旧版 {@code mybatis-plus-extension} 与新版 {@code mybatis-plus-spring},
+ * 子类都会被扫描到;行为与 {@code @MapperScan} 类似:只有标注了本注解的启动类/配置类才会触发扫描。
+ *
+ * 扫描由 {@link FastServiceScanRegistrar} 注册的 + * {@code com.mingliqiye.utils.springboot.autoconfigure.FastServiceScannerProcessor} 完成, + * 后者是 {@code BeanFactoryPostProcessor},会在所有 {@code BeanDefinitionRegistryPostProcessor} + * (包括 {@code @MapperScan} 的 {@code MapperScannerConfigurer})之后执行, + * 因此扫描注册时机在 mapper 之后,服务里的 {@code baseMapper} 可以正常注入。
+ * + *已经标注 {@code @Component} / {@code @Service} 的类会被跳过,交给普通组件扫描处理,避免重复注册。
+ * + *Java 用法:
+ *{@code
+ * @SpringBootApplication
+ * @MapperScan(basePackages = "com.example.mapper")
+ * @FastServiceScan(basePackages = "com.example.service")
+ * public class ExampleApplication {
+ * }
+ * }
+ *
+ * Kotlin 用法:
+ *{@code
+ * @SpringBootApplication
+ * @MapperScan(basePackages = ["com.example.mapper"])
+ * @FastServiceScan(basePackages = ["com.example.service"])
+ * class ExampleApplication
+ * }
+ *
+ * 不指定扫描包时,默认扫描标注类所在包。
+ *{@code Kotlin} 请指定 {@code Service} 为 {@code open}
+ */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(FastServiceScanRegistrar.class) +public @interface FastServiceScan { + + /** + * 要扫描的包,等价于 {@link #basePackages()}。 + * + * @return 要扫描的包 + */ + @AliasFor("basePackages") + String[] value() default {}; + + /** + * 要扫描的包。 + * + * @return 要扫描的包 + */ + + @AliasFor("value") + String[] basePackages() default {}; + + /** + * 以这些类所在包作为扫描包。 + * + * @return 用于推导扫描包的类 + */ + Class>[] basePackageClasses() default {}; +} diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationDestroyedRunner.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationDestroyedRunner.kt new file mode 100644 index 0000000..e49608d --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationDestroyedRunner.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile ApplicationDestroyedRunner.kt + * LastUpdate 2026-11-23 15:07:06 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.springboot.annotation + +import org.springframework.context.event.ContextClosedEvent +import org.springframework.context.event.EventListener +import org.springframework.core.Ordered +import org.springframework.core.annotation.AliasFor +import org.springframework.core.annotation.Order + +/** + * 应用销毁(上下文关闭)事件监听注解。 + * + * 标注在 Spring Bean 的方法上,当 Spring 应用上下文关闭 + * ([ContextClosedEvent] 发布)时自动调用该方法。 + * + * 已弃用,请改用 [ContextClosedEventListener](监听相同事件,命名更规范)。 + * + * @see ContextClosedEventListener + * @see EventListener + * @see ContextClosedEvent + */ +@Deprecated( + message = "已弃用,请改用 ContextClosedEventListener(监听相同事件,命名更规范)", + replaceWith = ReplaceWith( + expression = "ContextClosedEventListener", + imports = ["com.mingliqiye.utils.springboot.annotation.ContextClosedEventListener"], + ), +) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS) +@Retention(AnnotationRetention.RUNTIME) +@MustBeDocumented +@EventListener(ContextClosedEvent::class) +@Order +annotation class ApplicationDestroyedRunner( + /** + * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。 + * + * 默认为空字符串,表示无条件执行。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "condition") + val condition: String = "", + + /** + * 当事件不是 [ContextClosedEvent] 类型时是否仍默认执行监听方法。 + * + * 默认为 `true`。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "defaultExecution") + val defaultExecution: Boolean = true, + + /** + * 监听器的唯一标识,用于注册可指定 id 的监听器。 + * + * 默认为空字符串,表示自动生成。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "id") + val id: String = "", + + /** + * 监听器的执行顺序,值越小优先级越高。 + * + * 默认为 [Ordered.LOWEST_PRECEDENCE]。 + */ + @get:AliasFor(annotation = Order::class, attribute = "value") + val order: Int = Ordered.LOWEST_PRECEDENCE, +) diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationReadyEventListener.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationReadyEventListener.kt new file mode 100644 index 0000000..10c1dfb --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationReadyEventListener.kt @@ -0,0 +1,88 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile ApplicationReadyEventListener.kt + * LastUpdate 2026-11-23 15:07:06 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.springboot.annotation + +import org.springframework.boot.context.event.ApplicationReadyEvent +import org.springframework.context.event.EventListener +import org.springframework.core.Ordered +import org.springframework.core.annotation.AliasFor +import org.springframework.core.annotation.Order + +/** + * 应用就绪事件监听注解。 + * + * 标注在 Spring Bean 的方法上,当 Spring Boot 应用完成启动并就绪 + * ([ApplicationReadyEvent] 发布)时自动调用该方法, + * 等价于 `@EventListener(ApplicationReadyEvent::class)` 的组合简化写法。 + * + * ```kotlin + * @Component + * class InitTask { + * @ApplicationReadyEventListener(order = 0) + * fun onReady() { + * // 应用就绪后执行的初始化逻辑 + * } + * } + * ``` + * + * @see EventListener + * @see ApplicationReadyEvent + */ +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS) +@Retention(AnnotationRetention.RUNTIME) +@MustBeDocumented +@EventListener(ApplicationReadyEvent::class) +@Order +annotation class ApplicationReadyEventListener( + /** + * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。 + * + * 默认为空字符串,表示无条件执行。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "condition") + val condition: String = "", + + /** + * 当事件不是 [ApplicationReadyEvent] 类型时是否仍默认执行监听方法。 + * + * 默认为 `true`。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "defaultExecution") + val defaultExecution: Boolean = true, + + /** + * 监听器的唯一标识,用于注册可指定 id 的监听器。 + * + * 默认为空字符串,表示自动生成。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "id") + val id: String = "", + + /** + * 监听器的执行顺序,值越小优先级越高。 + * + * 默认为 [Ordered.LOWEST_PRECEDENCE]。 + */ + @get:AliasFor(annotation = Order::class, attribute = "value") + val order: Int = Ordered.LOWEST_PRECEDENCE, +) diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationStartedRunner.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationStartedRunner.kt new file mode 100644 index 0000000..d41d41c --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ApplicationStartedRunner.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile ApplicationStartedRunner.kt + * LastUpdate 2026-11-23 15:07:06 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.springboot.annotation + +import org.springframework.boot.context.event.ApplicationReadyEvent +import org.springframework.context.event.EventListener +import org.springframework.core.Ordered +import org.springframework.core.annotation.AliasFor +import org.springframework.core.annotation.Order + +/** + * 应用启动完成事件监听注解。 + * + * 标注在 Spring Bean 的方法上,当 Spring Boot 应用完成启动并就绪 + * ([ApplicationReadyEvent] 发布)时自动调用该方法。 + * + * 已弃用,请改用 [ApplicationReadyEventListener](功能完全一致,命名更规范)。 + * + * @see ApplicationReadyEventListener + * @see EventListener + * @see ApplicationReadyEvent + */ +@Deprecated( + message = "已弃用,请改用 ApplicationReadyEventListener(功能完全一致,命名更规范)", + replaceWith = ReplaceWith( + expression = "ApplicationReadyEventListener", + imports = ["com.mingliqiye.utils.springboot.annotation.ApplicationReadyEventListener"], + ), +) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS) +@Retention(AnnotationRetention.RUNTIME) +@MustBeDocumented +@EventListener(ApplicationReadyEvent::class) +@Order +annotation class ApplicationStartedRunner( + /** + * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。 + * + * 默认为空字符串,表示无条件执行。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "condition") + val condition: String = "", + + /** + * 当事件不是 [ApplicationReadyEvent] 类型时是否仍默认执行监听方法。 + * + * 默认为 `true`。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "defaultExecution") + val defaultExecution: Boolean = true, + + /** + * 监听器的唯一标识,用于注册可指定 id 的监听器。 + * + * 默认为空字符串,表示自动生成。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "id") + val id: String = "", + + /** + * 监听器的执行顺序,值越小优先级越高。 + * + * 默认为 [Ordered.LOWEST_PRECEDENCE]。 + */ + @get:AliasFor(annotation = Order::class, attribute = "value") + val order: Int = Ordered.LOWEST_PRECEDENCE, +) diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ContextClosedEventListener.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ContextClosedEventListener.kt new file mode 100644 index 0000000..59fba65 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/annotation/ContextClosedEventListener.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile ContextClosedEventListener.kt + * LastUpdate 2026-11-23 15:07:06 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.springboot.annotation + +import org.springframework.context.event.ContextClosedEvent +import org.springframework.context.event.EventListener +import org.springframework.core.Ordered +import org.springframework.core.annotation.AliasFor +import org.springframework.core.annotation.Order + +/** + * 应用上下文关闭事件监听注解。 + * + * 标注在 Spring Bean 的方法上,当 Spring 应用上下文关闭 + * ([ContextClosedEvent] 发布)时自动调用该方法, + * 等价于 `@EventListener(ContextClosedEvent::class)` 的组合简化写法, + * 适用于应用停机前的资源清理、状态保存等场景。 + * + * ```kotlin + * @Component + * class CleanupTask { + * @ContextClosedEventListener + * fun onClosed() { + * // 上下文关闭前执行的清理逻辑 + * } + * } + * ``` + * + * @see EventListener + * @see ContextClosedEvent + */ +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS) +@Retention(AnnotationRetention.RUNTIME) +@MustBeDocumented +@EventListener(ContextClosedEvent::class) +@Order +annotation class ContextClosedEventListener( + /** + * SpEL 条件表达式,仅当表达式求值为 true 时才执行监听方法。 + * + * 默认为空字符串,表示无条件执行。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "condition") + val condition: String = "", + + /** + * 当事件不是 [ContextClosedEvent] 类型时是否仍默认执行监听方法。 + * + * 默认为 `true`。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "defaultExecution") + val defaultExecution: Boolean = true, + + /** + * 监听器的唯一标识,用于注册可指定 id 的监听器。 + * + * 默认为空字符串,表示自动生成。 + */ + @get:AliasFor(annotation = EventListener::class, attribute = "id") + val id: String = "", + + /** + * 监听器的执行顺序,值越小优先级越高。 + * + * 默认为 [Ordered.LOWEST_PRECEDENCE]。 + */ + @get:AliasFor(annotation = Order::class, attribute = "value") + val order: Int = Ordered.LOWEST_PRECEDENCE, +) diff --git a/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/FastServiceScanRegistrar.kt b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/FastServiceScanRegistrar.kt new file mode 100644 index 0000000..d12bd18 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/springboot/autoconfigure/FastServiceScanRegistrar.kt @@ -0,0 +1,94 @@ +/* + * Copyright 2026 mingliqiye + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ProjectName mingli-utils + * ModuleName mingli-utils.main + * CurrentFile FastServiceScanRegistrar.kt + * LastUpdate 2026-11-23 14:21:18 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.springboot.autoconfigure + +import com.mingliqiye.utils.springboot.annotation.FastServiceScan +import org.springframework.beans.factory.config.BeanDefinition +import org.springframework.beans.factory.support.BeanDefinitionRegistry +import org.springframework.beans.factory.support.RootBeanDefinition +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar +import org.springframework.core.type.AnnotationMetadata +import org.springframework.util.ClassUtils + +/** + * [FastServiceScan] 的注册器:解析注解上的扫描包,并把 [FastServiceScannerProcessor] 注册成 Bean。 + * + * 通过 Spring 的 `MergedAnnotation.synthesize()` 读取注解,这样 `value` 与 `basePackages` + * 之间的 `@AliasFor` 会像 `@MapperScan` 一样被正确合并(两者同时指定不同值时也会按 Spring + * 的规则报错,而不是被静默合并)。 + * + * 这里不直接扫描,而是注册一个 `BeanFactoryPostProcessor`,由 Spring 在所有 + * `BeanDefinitionRegistryPostProcessor`(如 `@MapperScan` 的 `MapperScannerConfigurer`)之后执行, + * 从而保证扫描注册时机在 mapper 之后。 + */ +class FastServiceScanRegistrar : ImportBeanDefinitionRegistrar { + + override fun registerBeanDefinitions( + importingClassMetadata: AnnotationMetadata, + registry: BeanDefinitionRegistry, + ) { + val mergedAnnotation = importingClassMetadata.annotations.get(FastServiceScan::class.java) + if (!mergedAnnotation.isPresent) return + // synthesize 会应用 @AliasFor,value/basePackages 指向同一个值 + val annotation = mergedAnnotation.synthesize() + val packages = resolvePackages(annotation, importingClassMetadata) + if (packages.isEmpty()) return + + if (registry.containsBeanDefinition(PROCESSOR_BEAN_NAME)) return + + val definition = RootBeanDefinition(FastServiceScannerProcessor::class.java) + definition.constructorArgumentValues.addIndexedArgumentValue(0, packages.toTypedArray()) + definition.role = BeanDefinition.ROLE_INFRASTRUCTURE + registry.registerBeanDefinition(PROCESSOR_BEAN_NAME, definition) + } + + /** + * 解析注解上的扫描包;未指定时取标注类所在包。 + */ + private fun resolvePackages( + annotation: FastServiceScan, + importingClassMetadata: AnnotationMetadata, + ): Set