style: 全项目统一 Tab 缩进并补全尾随逗号
This commit is contained in:
+1
-1
@@ -24,5 +24,5 @@ package com.mingliqiye.utils.springboot.annotation
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
annotation class ApplicationDestroyedRunner(
|
||||
val order: Int = Int.MIN_VALUE
|
||||
val order: Int = Int.MIN_VALUE,
|
||||
)
|
||||
|
||||
+1
-1
@@ -24,5 +24,5 @@ package com.mingliqiye.utils.springboot.annotation
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
annotation class ApplicationStartedRunner(
|
||||
val order: Int = Int.MIN_VALUE,
|
||||
val order: Int = Int.MIN_VALUE,
|
||||
)
|
||||
|
||||
@@ -27,6 +27,6 @@ import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
annotation class Listener(
|
||||
@get:AliasFor(annotation = Component::class)
|
||||
val value: String = ""
|
||||
@get:AliasFor(annotation = Component::class)
|
||||
val value: String = "",
|
||||
)
|
||||
|
||||
+11
-11
@@ -32,18 +32,18 @@ import org.springframework.context.annotation.ComponentScan
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ComponentScan(
|
||||
"com.mingliqiye.utils.springboot.bean",
|
||||
"com.mingliqiye.utils.springboot.converters",
|
||||
"com.mingliqiye.utils.springboot.listener",
|
||||
"com.mingliqiye.utils.springboot.bean",
|
||||
"com.mingliqiye.utils.springboot.converters",
|
||||
"com.mingliqiye.utils.springboot.listener",
|
||||
)
|
||||
open class AutoConfiguration {
|
||||
|
||||
/**
|
||||
* 初始化块,在类实例化时执行。
|
||||
*
|
||||
* 调用printBanner方法打印启动横幅,并记录日志表示自动配置成功。
|
||||
*/
|
||||
init {
|
||||
com.mingliqiye.utils.main.Main.printb()
|
||||
}
|
||||
/**
|
||||
* 初始化块,在类实例化时执行。
|
||||
*
|
||||
* 调用printBanner方法打印启动横幅,并记录日志表示自动配置成功。
|
||||
*/
|
||||
init {
|
||||
com.mingliqiye.utils.main.Main.printb()
|
||||
}
|
||||
}
|
||||
|
||||
+31
-31
@@ -36,37 +36,37 @@ import org.springframework.context.annotation.Bean
|
||||
@AutoConfigureAfter(name = ["org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration"])
|
||||
open class Jackson3AutoConfiguration {
|
||||
|
||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3AutoConfiguration>()
|
||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3AutoConfiguration>()
|
||||
|
||||
/**
|
||||
* 注册 UUID 的 Jackson 3 序列化/反序列化模块。
|
||||
*
|
||||
* 如需替换 UUID 处理逻辑,注册名为 [UUIDJsonConverterJsonMapperCustomizer]
|
||||
* 的 [JsonMapperBuilderCustomizer] Bean 即可,本 Bean 将自动跳过。
|
||||
*/
|
||||
@Bean("UUIDJsonConverterJsonMapperCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["UUIDJsonConverterJsonMapperCustomizer"])
|
||||
open fun getUUIDJsonConverterJsonMapperCustomizer(): JsonMapperBuilderCustomizer {
|
||||
return JsonMapperBuilderCustomizer { builder ->
|
||||
builder.addModule(UUIDJsonConverter.getJsonConverter().getJackson3Module())
|
||||
}.also {
|
||||
logger.info("MingliUtils Jackson3 UUID Serializers created")
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 注册 UUID 的 Jackson 3 序列化/反序列化模块。
|
||||
*
|
||||
* 如需替换 UUID 处理逻辑,注册名为 [UUIDJsonConverterJsonMapperCustomizer]
|
||||
* 的 [JsonMapperBuilderCustomizer] Bean 即可,本 Bean 将自动跳过。
|
||||
*/
|
||||
@Bean("UUIDJsonConverterJsonMapperCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["UUIDJsonConverterJsonMapperCustomizer"])
|
||||
open fun getUUIDJsonConverterJsonMapperCustomizer(): JsonMapperBuilderCustomizer {
|
||||
return JsonMapperBuilderCustomizer { builder ->
|
||||
builder.addModule(UUIDJsonConverter.getJsonConverter().getJackson3Module())
|
||||
}.also {
|
||||
logger.info("MingliUtils Jackson3 UUID Serializers created")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册日期时间的 Jackson 3 序列化/反序列化模块。
|
||||
*
|
||||
* 如需替换日期时间处理逻辑,注册名为 [DateTimeJsonConverterJsonMapperCustomizer]
|
||||
* 的 [JsonMapperBuilderCustomizer] Bean 即可,本 Bean 将自动跳过。
|
||||
*/
|
||||
@Bean("DateTimeJsonConverterJsonMapperCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["DateTimeJsonConverterJsonMapperCustomizer"])
|
||||
open fun getDateTimeJsonConverterJsonMapperCustomizer(): JsonMapperBuilderCustomizer {
|
||||
return JsonMapperBuilderCustomizer { builder ->
|
||||
builder.addModule(DateTimeJsonConverter.getJsonConverter().getJackson3Module())
|
||||
}.also {
|
||||
logger.info("MingliUtils Jackson3 DateTime Serializers created")
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 注册日期时间的 Jackson 3 序列化/反序列化模块。
|
||||
*
|
||||
* 如需替换日期时间处理逻辑,注册名为 [DateTimeJsonConverterJsonMapperCustomizer]
|
||||
* 的 [JsonMapperBuilderCustomizer] Bean 即可,本 Bean 将自动跳过。
|
||||
*/
|
||||
@Bean("DateTimeJsonConverterJsonMapperCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["DateTimeJsonConverterJsonMapperCustomizer"])
|
||||
open fun getDateTimeJsonConverterJsonMapperCustomizer(): JsonMapperBuilderCustomizer {
|
||||
return JsonMapperBuilderCustomizer { builder ->
|
||||
builder.addModule(DateTimeJsonConverter.getJsonConverter().getJackson3Module())
|
||||
}.also {
|
||||
logger.info("MingliUtils Jackson3 DateTime Serializers created")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+37
-37
@@ -66,42 +66,42 @@ import tools.jackson.databind.json.JsonMapper
|
||||
@AutoConfigureAfter(name = ["com.mingliqiye.utils.springboot.autoconfigure.Jackson3AutoConfiguration"])
|
||||
open class Jackson3JsonApiAutoConfiguration {
|
||||
|
||||
companion object {
|
||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3JsonApiAutoConfiguration>()
|
||||
}
|
||||
companion object {
|
||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3JsonApiAutoConfiguration>()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并注册默认的 [JsonApi] Bean。
|
||||
*
|
||||
* 使用容器中的 [JsonMapper] 构造 [Jackson3JsonApi]。读取器直接返回该 [JsonMapper];
|
||||
* 写入器被设计为抛出 [JsonException],即本实现不允许通过 [JsonApi] 替换底层
|
||||
* `JsonMapper`,底层实例由 Spring 容器统一管理。
|
||||
*
|
||||
* 创建完成后会尝试初始化全局 [JSONA]:
|
||||
* - 若 [JSONA] 尚未设置,则设置为本实例;
|
||||
* - 若已设置,则保留既有实现。
|
||||
*
|
||||
* 该 Bean 仅在容器中不存在其他 [JsonApi] 时创建。
|
||||
*
|
||||
* @param jsonMapper 容器中的 Jackson 3 映射器
|
||||
* @return 基于 Jackson 3 的 JSON 访问门面
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(JsonApi::class)
|
||||
open fun jsonApi(jsonMapper: JsonMapper): JsonApi {
|
||||
return Jackson3JsonApi(
|
||||
get = { jsonMapper },
|
||||
set = { throw JsonException("JSON API set method is not allowed.") })
|
||||
.also {
|
||||
logger.info("JsonApi Bean Auto Configuration Success")
|
||||
try {
|
||||
JSONA.getJsonApi()
|
||||
} catch (_: NullPointerException) {
|
||||
JSONA.setJsonApi(it)
|
||||
logger.info(
|
||||
"JSONA Using {}", it.javaClass.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 创建并注册默认的 [JsonApi] Bean。
|
||||
*
|
||||
* 使用容器中的 [JsonMapper] 构造 [Jackson3JsonApi]。读取器直接返回该 [JsonMapper];
|
||||
* 写入器被设计为抛出 [JsonException],即本实现不允许通过 [JsonApi] 替换底层
|
||||
* `JsonMapper`,底层实例由 Spring 容器统一管理。
|
||||
*
|
||||
* 创建完成后会尝试初始化全局 [JSONA]:
|
||||
* - 若 [JSONA] 尚未设置,则设置为本实例;
|
||||
* - 若已设置,则保留既有实现。
|
||||
*
|
||||
* 该 Bean 仅在容器中不存在其他 [JsonApi] 时创建。
|
||||
*
|
||||
* @param jsonMapper 容器中的 Jackson 3 映射器
|
||||
* @return 基于 Jackson 3 的 JSON 访问门面
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(JsonApi::class)
|
||||
open fun jsonApi(jsonMapper: JsonMapper): JsonApi {
|
||||
return Jackson3JsonApi(
|
||||
get = { jsonMapper },
|
||||
set = { throw JsonException("JSON API set method is not allowed.") })
|
||||
.also {
|
||||
logger.info("JsonApi Bean Auto Configuration Success")
|
||||
try {
|
||||
JSONA.getJsonApi()
|
||||
} catch (_: NullPointerException) {
|
||||
JSONA.setJsonApi(it)
|
||||
logger.info(
|
||||
"JSONA Using {}", it.javaClass.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+26
-26
@@ -46,32 +46,32 @@ import org.springframework.context.annotation.Primary
|
||||
open class JacksonJsonApiAutoConfiguration {
|
||||
|
||||
|
||||
companion object {
|
||||
private val log: Logger = MingLiLoggerFactory.getLogger("MingliUtils-JacksonAutoConfiguration")
|
||||
}
|
||||
companion object {
|
||||
private val log: Logger = MingLiLoggerFactory.getLogger("MingliUtils-JacksonAutoConfiguration")
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 创建并返回一个 JsonApi Bean 实例。
|
||||
* 该方法会在没有其他 JsonApi Bean 存在时被调用,并将创建的 JacksonJsonApi 实例设置为全局默认的 JSON API。
|
||||
*
|
||||
* @param objectMapper 用于 JSON 操作的 ObjectMapper 实例。
|
||||
* @return 返回配置好的 JsonApi 实例。
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(JsonApi::class)
|
||||
open fun jsonApi(objectMapper: ObjectMapper): JsonApi {
|
||||
return JacksonJsonApi(objectMapper).also {
|
||||
log.info("JsonApi Bean Auto Configuration Success")
|
||||
try {
|
||||
JSONA.getJsonApi()
|
||||
} catch (_: NullPointerException) {
|
||||
JSONA.setJsonApi(it)
|
||||
log.info(
|
||||
"JSONA Using {}", it.javaClass.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 创建并返回一个 JsonApi Bean 实例。
|
||||
* 该方法会在没有其他 JsonApi Bean 存在时被调用,并将创建的 JacksonJsonApi 实例设置为全局默认的 JSON API。
|
||||
*
|
||||
* @param objectMapper 用于 JSON 操作的 ObjectMapper 实例。
|
||||
* @return 返回配置好的 JsonApi 实例。
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(JsonApi::class)
|
||||
open fun jsonApi(objectMapper: ObjectMapper): JsonApi {
|
||||
return JacksonJsonApi(objectMapper).also {
|
||||
log.info("JsonApi Bean Auto Configuration Success")
|
||||
try {
|
||||
JSONA.getJsonApi()
|
||||
} catch (_: NullPointerException) {
|
||||
JSONA.setJsonApi(it)
|
||||
log.info(
|
||||
"JSONA Using {}", it.javaClass.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+23
-23
@@ -63,30 +63,30 @@ import org.springframework.context.annotation.Bean
|
||||
@AutoConfigureAfter(name = ["org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
|
||||
open class JacksonJsonAutoConfiguration {
|
||||
|
||||
companion object {
|
||||
private val log = MingLiLoggerFactory.getLogger<JacksonJsonAutoConfiguration>()
|
||||
}
|
||||
companion object {
|
||||
private val log = MingLiLoggerFactory.getLogger<JacksonJsonAutoConfiguration>()
|
||||
}
|
||||
|
||||
@Bean("DateTimeJsonConverterJackson2ObjectMapperBuilderCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["DateTimeJsonConverterJackson2ObjectMapperBuilderCustomizer"])
|
||||
fun getDateTimeJsonConverterJackson2ObjectMapperBuilderCustomizer() = Jackson2ObjectMapperBuilderCustomizer {
|
||||
log.info(
|
||||
"MingliUtils Jackson2 DateTime Serializers created"
|
||||
)
|
||||
it.modulesToInstall { modules ->
|
||||
modules.add(DateTimeJsonConverter.getJsonConverter().getJacksonModule())
|
||||
}
|
||||
}
|
||||
@Bean("DateTimeJsonConverterJackson2ObjectMapperBuilderCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["DateTimeJsonConverterJackson2ObjectMapperBuilderCustomizer"])
|
||||
fun getDateTimeJsonConverterJackson2ObjectMapperBuilderCustomizer() = Jackson2ObjectMapperBuilderCustomizer {
|
||||
log.info(
|
||||
"MingliUtils Jackson2 DateTime Serializers created"
|
||||
)
|
||||
it.modulesToInstall { modules ->
|
||||
modules.add(DateTimeJsonConverter.getJsonConverter().getJacksonModule())
|
||||
}
|
||||
}
|
||||
|
||||
@Bean("UUIDJsonConverterJackson2ObjectMapperBuilderCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["UUIDJsonConverterJackson2ObjectMapperBuilderCustomizer"])
|
||||
fun getUUIDJsonConverterJackson2ObjectMapperBuilderCustomizer() = Jackson2ObjectMapperBuilderCustomizer {
|
||||
log.info(
|
||||
"MingliUtils Jackson2 UUID Serializers created"
|
||||
)
|
||||
it.modulesToInstall { modules ->
|
||||
modules.add(UUIDJsonConverter.getJsonConverter().getJacksonModule())
|
||||
}
|
||||
}
|
||||
@Bean("UUIDJsonConverterJackson2ObjectMapperBuilderCustomizer")
|
||||
@ConditionalOnMissingBean(name = ["UUIDJsonConverterJackson2ObjectMapperBuilderCustomizer"])
|
||||
fun getUUIDJsonConverterJackson2ObjectMapperBuilderCustomizer() = Jackson2ObjectMapperBuilderCustomizer {
|
||||
log.info(
|
||||
"MingliUtils Jackson2 UUID Serializers created"
|
||||
)
|
||||
it.modulesToInstall { modules ->
|
||||
modules.add(UUIDJsonConverter.getJsonConverter().getJacksonModule())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+56
-56
@@ -61,63 +61,63 @@ import org.springframework.context.event.EventListener
|
||||
@ConditionalOnClass(name = ["okhttp3.OkHttpClient"])
|
||||
open class Okhttp3AutoConfiguration {
|
||||
|
||||
companion object {
|
||||
val log: Logger = MingLiLoggerFactory.getLogger<Okhttp3AutoConfiguration>()
|
||||
}
|
||||
companion object {
|
||||
val log: Logger = MingLiLoggerFactory.getLogger<Okhttp3AutoConfiguration>()
|
||||
}
|
||||
|
||||
/**
|
||||
* 由本配置创建并注册的 [okhttp3.OkHttpClient] 实例,供上下文关闭时清理使用。
|
||||
*
|
||||
* 当调用方自行声明了 `OkHttpClient` Bean 时,本配置的 Bean 方法不会执行,
|
||||
* 该字段保持 `null`,关闭时的清理逻辑也会相应跳过。
|
||||
*/
|
||||
var okhttp: okhttp3.OkHttpClient? = null
|
||||
/**
|
||||
* 由本配置创建并注册的 [okhttp3.OkHttpClient] 实例,供上下文关闭时清理使用。
|
||||
*
|
||||
* 当调用方自行声明了 `OkHttpClient` Bean 时,本配置的 Bean 方法不会执行,
|
||||
* 该字段保持 `null`,关闭时的清理逻辑也会相应跳过。
|
||||
*/
|
||||
var okhttp: okhttp3.OkHttpClient? = null
|
||||
|
||||
/**
|
||||
* 创建默认的 [okhttp3.OkHttpClient] Bean。
|
||||
*
|
||||
* 依次应用容器中所有 [OkhttpBuilder] 定制器后构建客户端。没有任何定制器时
|
||||
* 也能正常构建([ObjectProvider] 允许零个元素)。
|
||||
*
|
||||
* 该 Bean 仅在容器中不存在其他 `OkHttpClient` 时创建;若调用方已自行声明,
|
||||
* 本方法不会被调用。
|
||||
*
|
||||
* @param builders 容器中的 OkHttp 构建定制器,按 `@Order` 排序后依次应用
|
||||
* @return 构建完成的 OkHttp 客户端
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
open fun okHttpClient(builders: ObjectProvider<OkhttpBuilder>): okhttp3.OkHttpClient {
|
||||
var connt = 0
|
||||
return okhttp3.OkHttpClient.Builder().let { builder ->
|
||||
var b = builder
|
||||
builders.orderedStream().forEach {
|
||||
b = it.customize(b)
|
||||
connt++
|
||||
}
|
||||
b.build()
|
||||
}.also {
|
||||
okhttp = it
|
||||
log.info("load $connt OkhttpBuilder initialized OkHttpClient successfully")
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 创建默认的 [okhttp3.OkHttpClient] Bean。
|
||||
*
|
||||
* 依次应用容器中所有 [OkhttpBuilder] 定制器后构建客户端。没有任何定制器时
|
||||
* 也能正常构建([ObjectProvider] 允许零个元素)。
|
||||
*
|
||||
* 该 Bean 仅在容器中不存在其他 `OkHttpClient` 时创建;若调用方已自行声明,
|
||||
* 本方法不会被调用。
|
||||
*
|
||||
* @param builders 容器中的 OkHttp 构建定制器,按 `@Order` 排序后依次应用
|
||||
* @return 构建完成的 OkHttp 客户端
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
open fun okHttpClient(builders: ObjectProvider<OkhttpBuilder>): okhttp3.OkHttpClient {
|
||||
var connt = 0
|
||||
return okhttp3.OkHttpClient.Builder().let { builder ->
|
||||
var b = builder
|
||||
builders.orderedStream().forEach {
|
||||
b = it.customize(b)
|
||||
connt++
|
||||
}
|
||||
b.build()
|
||||
}.also {
|
||||
okhttp = it
|
||||
log.info("load $connt OkhttpBuilder initialized OkHttpClient successfully")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 Spring 上下文关闭时清理本配置创建的 OkHttp 客户端。
|
||||
*
|
||||
* 执行两步清理:
|
||||
* 1. [okhttp3.ConnectionPool.evictAll] 立即关闭连接池中的空闲连接;
|
||||
* 2. 关闭 [okhttp3.Dispatcher] 的线程池,中断未完成的任务。
|
||||
*
|
||||
* 仅当 [okhttp] 字段非空(即客户端由本配置创建)时执行;
|
||||
* 调用方自行声明的客户端不会被清理。
|
||||
*/
|
||||
@EventListener(ContextClosedEvent::class)
|
||||
open fun handleContextClosed() {
|
||||
okhttp?.let {
|
||||
log.info("Context closed, cleaning up OkHttpClient...")
|
||||
it.connectionPool.evictAll()
|
||||
it.dispatcher.executorService.shutdownNow()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 在 Spring 上下文关闭时清理本配置创建的 OkHttp 客户端。
|
||||
*
|
||||
* 执行两步清理:
|
||||
* 1. [okhttp3.ConnectionPool.evictAll] 立即关闭连接池中的空闲连接;
|
||||
* 2. 关闭 [okhttp3.Dispatcher] 的线程池,中断未完成的任务。
|
||||
*
|
||||
* 仅当 [okhttp] 字段非空(即客户端由本配置创建)时执行;
|
||||
* 调用方自行声明的客户端不会被清理。
|
||||
*/
|
||||
@EventListener(ContextClosedEvent::class)
|
||||
open fun handleContextClosed() {
|
||||
okhttp?.let {
|
||||
log.info("Context closed, cleaning up OkHttpClient...")
|
||||
it.connectionPool.evictAll()
|
||||
it.dispatcher.executorService.shutdownNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,5 +26,5 @@ import okhttp3.OkHttpClient
|
||||
|
||||
@FunctionalInterface
|
||||
fun interface OkhttpBuilder {
|
||||
fun customize(builder: OkHttpClient.Builder): OkHttpClient.Builder
|
||||
fun customize(builder: OkHttpClient.Builder): OkHttpClient.Builder
|
||||
}
|
||||
|
||||
@@ -35,75 +35,75 @@ import org.springframework.stereotype.Component
|
||||
@Component
|
||||
class SpringBeanUtils : ApplicationContextAware {
|
||||
|
||||
companion object {
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
val applicationContext: ApplicationContext
|
||||
get() = applicationContextBase
|
||||
?: throw ApplicationContextException("applicationContext isn't ContextInitialized ?")
|
||||
@JvmStatic
|
||||
val applicationContext: ApplicationContext
|
||||
get() = applicationContextBase
|
||||
?: throw ApplicationContextException("applicationContext isn't ContextInitialized ?")
|
||||
|
||||
private var applicationContextBase: ApplicationContext? = null
|
||||
private var applicationContextBase: ApplicationContext? = null
|
||||
|
||||
inline fun <reified T : Any> lazyBean(): Lazy<T> = lazy(::getBean)
|
||||
inline fun <reified T : Any> lazyBean(): Lazy<T> = lazy(::getBean)
|
||||
|
||||
inline fun <reified T : Any> lazyBean(name: String): Lazy<T> = lazy {
|
||||
getBean(name, T::class.java)
|
||||
}
|
||||
inline fun <reified T : Any> lazyBean(name: String): Lazy<T> = lazy {
|
||||
getBean(name, T::class.java)
|
||||
}
|
||||
|
||||
|
||||
@Throws(BeansException::class)
|
||||
inline fun <reified T : Any> getBean() = getBean(T::class.java)
|
||||
@Throws(BeansException::class)
|
||||
inline fun <reified T : Any> getBean() = getBean(T::class.java)
|
||||
|
||||
/**
|
||||
* 根据Bean名称获取Bean实例
|
||||
*
|
||||
* @param name Bean的名称
|
||||
* @return 指定名称的Bean实例
|
||||
* @throws BeansException 当获取Bean失败时抛出
|
||||
* @throws ClassCastException 当类型转换失败时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun getBean(name: String): Any = applicationContext.getBean(name)
|
||||
/**
|
||||
* 根据Bean名称获取Bean实例
|
||||
*
|
||||
* @param name Bean的名称
|
||||
* @return 指定名称的Bean实例
|
||||
* @throws BeansException 当获取Bean失败时抛出
|
||||
* @throws ClassCastException 当类型转换失败时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun getBean(name: String): Any = applicationContext.getBean(name)
|
||||
|
||||
/**
|
||||
* 根据Bean类型获取Bean实例
|
||||
*
|
||||
* @param clazz Bean的类型
|
||||
* @return 指定类型的Bean实例
|
||||
* @throws BeansException 当获取Bean失败时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
fun <T : Any> getBean(clazz: Class<T>): T {
|
||||
return applicationContext.getBean(clazz)
|
||||
}
|
||||
/**
|
||||
* 根据Bean类型获取Bean实例
|
||||
*
|
||||
* @param clazz Bean的类型
|
||||
* @return 指定类型的Bean实例
|
||||
* @throws BeansException 当获取Bean失败时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
fun <T : Any> getBean(clazz: Class<T>): T {
|
||||
return applicationContext.getBean(clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Bean名称和类型获取Bean实例
|
||||
*
|
||||
* @param name Bean的名称
|
||||
* @param clazz Bean的类型
|
||||
* @return 指定名称和类型的Bean实例
|
||||
* @throws BeansException 当获取Bean失败时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
fun <T : Any> getBean(name: String, clazz: Class<T>): T {
|
||||
return applicationContext.getBean<T>(name, clazz)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 根据Bean名称和类型获取Bean实例
|
||||
*
|
||||
* @param name Bean的名称
|
||||
* @param clazz Bean的类型
|
||||
* @return 指定名称和类型的Bean实例
|
||||
* @throws BeansException 当获取Bean失败时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(BeansException::class)
|
||||
fun <T : Any> getBean(name: String, clazz: Class<T>): T {
|
||||
return applicationContext.getBean<T>(name, clazz)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置Spring的应用上下文
|
||||
*
|
||||
* @param applicationContext Spring的应用上下文
|
||||
* @throws BeansException 当设置应用上下文失败时抛出
|
||||
*/
|
||||
@Throws(BeansException::class)
|
||||
override fun setApplicationContext(applicationContext: ApplicationContext) {
|
||||
applicationContextBase = applicationContext
|
||||
}
|
||||
/**
|
||||
* 设置Spring的应用上下文
|
||||
*
|
||||
* @param applicationContext Spring的应用上下文
|
||||
* @throws BeansException 当设置应用上下文失败时抛出
|
||||
*/
|
||||
@Throws(BeansException::class)
|
||||
override fun setApplicationContext(applicationContext: ApplicationContext) {
|
||||
applicationContextBase = applicationContext
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -29,7 +29,7 @@ import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class DateTimeToStringConverter : Converter<DateTime, String?> {
|
||||
override fun convert(source: DateTime): String {
|
||||
return source.format(Formatter.STANDARD_DATETIME)
|
||||
}
|
||||
override fun convert(source: DateTime): String {
|
||||
return source.format(Formatter.STANDARD_DATETIME)
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -29,7 +29,7 @@ import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class StringToDateTimeConverter : Converter<String, DateTime> {
|
||||
override fun convert(source: String): DateTime {
|
||||
return DateTime.parse(source, Formatter.STANDARD_DATETIME_MILLISECOUND7, true)
|
||||
}
|
||||
override fun convert(source: String): DateTime {
|
||||
return DateTime.parse(source, Formatter.STANDARD_DATETIME_MILLISECOUND7, true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class StringToUUIDConverter : Converter<String, UUID> {
|
||||
override fun convert(source: String): UUID {
|
||||
return UUID.of(source)
|
||||
}
|
||||
override fun convert(source: String): UUID {
|
||||
return UUID.of(source)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class UUIDToStringConverter : Converter<UUID, String> {
|
||||
override fun convert(source: UUID): String {
|
||||
return source.getString()
|
||||
}
|
||||
override fun convert(source: UUID): String {
|
||||
return source.getString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,70 +36,70 @@ import org.springframework.context.event.EventListener
|
||||
@Listener
|
||||
class ApplicationListener {
|
||||
|
||||
val runningMap = HashMap<String, MutableList<ApplicationRunnerData>>()
|
||||
val runningMap = HashMap<String, MutableList<ApplicationRunnerData>>()
|
||||
|
||||
companion object {
|
||||
private val log = MingLiLoggerFactory.getLogger<ApplicationListener>()
|
||||
}
|
||||
companion object {
|
||||
private val log = MingLiLoggerFactory.getLogger<ApplicationListener>()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@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()
|
||||
}
|
||||
}
|
||||
@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()
|
||||
}
|
||||
}
|
||||
|
||||
@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()
|
||||
}
|
||||
}
|
||||
@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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ package com.mingliqiye.utils.springboot.listener
|
||||
import java.lang.reflect.Method
|
||||
|
||||
data class ApplicationRunnerData(
|
||||
val beanName: String, val order: Int, val bean: Any, val method: Method
|
||||
val beanName: String, val order: Int, val bean: Any, val method: Method,
|
||||
) {
|
||||
fun run() {
|
||||
method.invoke(bean)
|
||||
}
|
||||
fun run() {
|
||||
method.invoke(bean)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user