refactor(springboot): 事件监听注解迁移为 Java 实现
新增 ApplicationReadyEventListener、ContextClosedEventListener; ApplicationStartedRunner、ApplicationDestroyedRunner 标记废弃; 移除 Kotlin 版注解与 ApplicationListener、ApplicationRunnerData; Okhttp3AutoConfiguration 改用 @ContextClosedEventListener。
This commit is contained in:
-36
@@ -1,36 +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.kt
|
||||
* LastUpdate 2026-03-10 09:04:02
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.springboot.annotation
|
||||
|
||||
/**
|
||||
* 标记在 Spring 上下文关闭时执行的方法。
|
||||
* 被标注的方法会在 ContextClosedEvent 触发时由
|
||||
* [com.mingliqiye.utils.springboot.listener.ApplicationListener] 反射调用,
|
||||
* 多个方法按 [order] 升序执行。
|
||||
*
|
||||
* @property order 执行顺序,值越小越先执行,默认为 [Int.MIN_VALUE]
|
||||
*/
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
annotation class ApplicationDestroyedRunner(
|
||||
val order: Int = Int.MIN_VALUE,
|
||||
)
|
||||
-36
@@ -1,36 +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.kt
|
||||
* LastUpdate 2026-03-11 08:46:40
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.springboot.annotation
|
||||
|
||||
/**
|
||||
* 标记在 Spring Boot 应用启动完成后执行的方法。
|
||||
* 被标注的方法会在 ApplicationStartedEvent 触发时由
|
||||
* [com.mingliqiye.utils.springboot.listener.ApplicationListener] 反射调用,
|
||||
* 多个方法按 [order] 升序执行。
|
||||
*
|
||||
* @property order 执行顺序,值越小越先执行,默认为 [Int.MIN_VALUE]
|
||||
*/
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
annotation class ApplicationStartedRunner(
|
||||
val order: Int = Int.MIN_VALUE,
|
||||
)
|
||||
@@ -1,39 +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 Listener.kt
|
||||
* LastUpdate 2026-03-10 09:04:02
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.springboot.annotation
|
||||
|
||||
import org.springframework.core.annotation.AliasFor
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
* 监听器组件注解,组合了 [Component] 的语义。
|
||||
* 被标注的类会被组件扫描注册为 Spring Bean,
|
||||
* [value] 通过 [AliasFor] 别名到 [Component.value],用于指定 Bean 名称。
|
||||
*
|
||||
* @property value Bean 名称,默认为空字符串(由 Spring 按类名生成)
|
||||
*/
|
||||
@Component
|
||||
annotation class Listener(
|
||||
@get:AliasFor(annotation = Component::class)
|
||||
val value: String = "",
|
||||
)
|
||||
+3
-3
@@ -16,13 +16,14 @@
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile Okhttp3AutoConfiguration.kt
|
||||
* LastUpdate 2026-11-16 11:29:35
|
||||
* LastUpdate 2026-11-18 08:22:35
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.springboot.autoconfigure
|
||||
|
||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||
import com.mingliqiye.utils.springboot.annotation.ContextClosedEventListener
|
||||
import com.mingliqiye.utils.springboot.autoconfigure.builder.OkhttpBuilder
|
||||
import org.slf4j.Logger
|
||||
import org.springframework.beans.factory.ObjectProvider
|
||||
@@ -31,7 +32,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.event.ContextClosedEvent
|
||||
import org.springframework.context.event.EventListener
|
||||
|
||||
/**
|
||||
* OkHttp 3 的 Spring Boot 自动配置。
|
||||
@@ -118,7 +118,7 @@ open class Okhttp3AutoConfiguration {
|
||||
* 仅当 [okhttp] 字段非空(即客户端由本配置创建)时执行;
|
||||
* 调用方自行声明的客户端不会被清理。
|
||||
*/
|
||||
@EventListener(ContextClosedEvent::class)
|
||||
@ContextClosedEventListener
|
||||
open fun handleContextClosed() {
|
||||
okhttp?.let {
|
||||
log.info("Context closed, cleaning up OkHttpClient...")
|
||||
|
||||
@@ -1,132 +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 ApplicationListener.kt
|
||||
* LastUpdate 2026-03-11 08:20:37
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.springboot.listener
|
||||
|
||||
|
||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||
import com.mingliqiye.utils.springboot.annotation.ApplicationDestroyedRunner
|
||||
import com.mingliqiye.utils.springboot.annotation.ApplicationStartedRunner
|
||||
import com.mingliqiye.utils.springboot.annotation.Listener
|
||||
import org.springframework.beans.factory.ListableBeanFactory
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent
|
||||
import org.springframework.context.event.ContextClosedEvent
|
||||
import org.springframework.context.event.EventListener
|
||||
|
||||
|
||||
/**
|
||||
* 应用生命周期监听器。
|
||||
* 在应用启动完成时扫描容器中所有 Bean 的方法,收集标注了
|
||||
* [ApplicationStartedRunner] / [ApplicationDestroyedRunner] 的方法并按 order 排序,
|
||||
* 分别在 ApplicationStartedEvent 和 ContextClosedEvent 触发时依次反射执行。
|
||||
*/
|
||||
@Listener
|
||||
class ApplicationListener {
|
||||
|
||||
/**
|
||||
* 已扫描到的待执行方法集合,键为注解类全名,值为按 order 升序排列的执行数据列表。
|
||||
*/
|
||||
val runningMap = HashMap<String, MutableList<ApplicationRunnerData>>()
|
||||
|
||||
/**
|
||||
* 伴生对象,持有本监听器的日志记录器。
|
||||
*/
|
||||
companion object {
|
||||
private val log = MingLiLoggerFactory.getLogger<ApplicationListener>()
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描容器中的 Bean 方法,收集生命周期回调并按 order 排序。
|
||||
*
|
||||
* 跳过 `org.springframework` 包下的 Bean;每次调用会先清空 [runningMap] 再重新扫描。
|
||||
*
|
||||
* @param applicationContext 用于遍历 Bean 定义与获取 Bean 实例的容器
|
||||
*/
|
||||
fun init(applicationContext: ListableBeanFactory) {
|
||||
runningMap.clear()
|
||||
for (beanName in applicationContext.beanDefinitionNames) {
|
||||
val bean: Any = applicationContext.getBean(beanName)
|
||||
val beanClass: Class<*> = bean.javaClass
|
||||
if (beanClass.getName().startsWith("org.springframework")) {
|
||||
continue
|
||||
}
|
||||
for (method in beanClass.getDeclaredMethods()) {
|
||||
val annotation: Any =
|
||||
method.getAnnotation(ApplicationStartedRunner::class.java) ?: method.getAnnotation(
|
||||
ApplicationDestroyedRunner::class.java
|
||||
) ?: continue
|
||||
var order: Int
|
||||
var key: Class<out Annotation>
|
||||
when (annotation) {
|
||||
is ApplicationStartedRunner -> {
|
||||
order = annotation.order
|
||||
key = ApplicationStartedRunner::class.java
|
||||
}
|
||||
|
||||
is ApplicationDestroyedRunner -> {
|
||||
order = annotation.order
|
||||
key = ApplicationDestroyedRunner::class.java
|
||||
}
|
||||
|
||||
else -> continue
|
||||
}
|
||||
log.debug("ApplicationListener Scaned [{} - {}] order: {} ", bean.javaClass.name, method, order)
|
||||
val d = runningMap.getOrPut(key.name) {
|
||||
ArrayList()
|
||||
}
|
||||
d.add(
|
||||
ApplicationRunnerData(
|
||||
beanName = beanName, order = order, bean = bean, method = method
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
runningMap.forEach { (string, data) ->
|
||||
data.sortWith(compareBy(ApplicationRunnerData::order))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用启动完成事件回调:先执行 [init] 扫描,再按 order 升序执行所有 [ApplicationStartedRunner] 方法。
|
||||
*
|
||||
* @param event 应用启动完成事件
|
||||
*/
|
||||
@EventListener(ApplicationStartedEvent::class)
|
||||
fun applicationStarted(event: ApplicationStartedEvent) {
|
||||
init(event.applicationContext)
|
||||
runningMap[ApplicationStartedRunner::class.java.name]?.forEach {
|
||||
log.debug("Started Event Running {} {} {} - {}", it.beanName, it.bean, it.method, it.order)
|
||||
it.run()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文关闭事件回调:按 order 升序执行所有 [ApplicationDestroyedRunner] 方法。
|
||||
*/
|
||||
@EventListener(ContextClosedEvent::class)
|
||||
fun applicationClosed() {
|
||||
runningMap[ApplicationDestroyedRunner::class.java.name]?.forEach {
|
||||
log.debug("Closed Event Running {} {} {} - {}", it.beanName, it.bean, it.method, it.order)
|
||||
it.run()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +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 ApplicationRunnerData.kt
|
||||
* LastUpdate 2026-03-10 09:10:50
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.springboot.listener
|
||||
|
||||
import java.lang.reflect.Method
|
||||
|
||||
/**
|
||||
* 生命周期回调的执行数据,记录一个待反射执行的方法及其宿主 Bean 与执行顺序。
|
||||
*
|
||||
* @property beanName Bean 在容器中的名称
|
||||
* @property order 执行顺序,值越小越先执行
|
||||
* @property bean 方法所属的 Bean 实例
|
||||
* @property method 待执行的回调方法
|
||||
*/
|
||||
data class ApplicationRunnerData(
|
||||
val beanName: String, val order: Int, val bean: Any, val method: Method,
|
||||
) {
|
||||
/**
|
||||
* 反射调用 [method],以 [bean] 为宿主实例执行回调。
|
||||
*/
|
||||
fun run() {
|
||||
method.invoke(bean)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user