no message

This commit is contained in:
2026-02-05 11:42:52 +08:00
parent f2ee83ca64
commit a5a532bc82
90 changed files with 7945 additions and 2458 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2025 mingliqiye
* 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.
@@ -16,13 +16,13 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile AutoConfiguration.kt
* LastUpdate 2025-09-20 10:47:00
* LastUpdate 2026-02-05 10:45:05
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.springboot.autoconfigure
import com.mingliqiye.utils.logger.mingLiLoggerFactory
import com.mingliqiye.utils.logger.MingLiLoggerFactory
import com.mingliqiye.utils.system.computerName
import com.mingliqiye.utils.system.getPid
import com.mingliqiye.utils.system.jdkVersion
@@ -32,14 +32,24 @@ import com.mingliqiye.utils.time.Formatter
import org.springframework.context.annotation.ComponentScan
import java.io.IOException
/**
* 自动配置类,用于Spring Boot应用启动时加载相关组件和打印启动横幅。
*
* 该类通过@ComponentScan注解扫描指定包下的Bean,并在初始化时打印包含系统信息的启动横幅。
*/
@org.springframework.boot.autoconfigure.AutoConfiguration
@ComponentScan(
"com.mingliqiye.utils.springboot.bean",
"com.mingliqiye.utils.springboot.converters"
)
open class AutoConfiguration {
private val log = MingLiLoggerFactory.getLogger("MingliUtils-AutoConfiguration")
companion object {
private const val banner =
/**
* 启动横幅字符串,包含艺术字体和占位符。
*/
private const val BANNER =
"---------------------------------------------------------\n" +
"| $$\\ $$\\ $$\\ $$\\ $$\\ $$$$$$$$\\ $$$$$$\\ |\n" +
"| $$$\\ $$$ |$$ | $$ | $$ |\\__$$ __|$$ __$$\\ |\n" +
@@ -49,25 +59,38 @@ open class AutoConfiguration {
"| $$ |\\$ /$$ |$$ | $$ | $$ | $$ | $$\\ $$ | |\n" +
"| $$ | \\_/ $$ |$$$$$$$$\\\\$$$$$$ | $$ | \\$$$$$$ | |\n" +
"| \\__| \\__|\\________|\\______/ \\__| \\______/ |\n"
private val log = mingLiLoggerFactory.getLogger("MingliUtils-AutoConfiguration")
/**
* 打印启动横幅,包含系统元数据(如JDK版本、进程ID、计算机名等)。
*
* 该方法从资源文件中读取元数据,并将其格式化后追加到横幅中进行输出。
* 如果读取资源失败,则仅打印默认横幅。
*/
fun printBanner() {
val bannerBuilder = StringBuilder(banner)
val bannerBuilder = StringBuilder(BANNER)
// 尝试从资源文件中读取元数据并拼接到横幅中
try {
val inputStream = AutoConfiguration::class.java.getResourceAsStream("/META-INF/meta-data") ?: return
inputStream.use { stream ->
var readlen: Int
val buffer = ByteArray(1024)
val metaData = StringBuilder()
// 逐块读取资源文件内容
while (stream.read(buffer).also { readlen = it } != -1) {
metaData.append(String(buffer, 0, readlen))
}
// 解析元数据并添加额外的系统信息
val da = metaData.toString().split("\n").toMutableList()
da.add("jdkRuntime=$jdkVersion")
da.add("pid=$getPid")
da.add("computerName=$computerName")
da.add("userName=$userName")
da.add("time=" + DateTime.now().format(Formatter.STANDARD_DATETIME_MILLISECOUND7, true))
da.add("time=" + DateTime.now().format(Formatter.STANDARD_DATETIME_MILLISECOUND7, false))
// 格式化每条元数据并追加到横幅中
da.forEach { s: String ->
val d = s.trim { it <= ' ' }.split("=".toRegex(), 2).toTypedArray()
if (d.size >= 2) {
@@ -89,15 +112,23 @@ open class AutoConfiguration {
}
}
} catch (e: IOException) {
// 捕获IO异常并打印堆栈跟踪
e.printStackTrace()
}
// 输出最终构建的横幅
println(bannerBuilder.toString().trim())
println("---------------------------------------------------------")
}
}
/**
* 初始化块,在类实例化时执行。
*
* 调用printBanner方法打印启动横幅,并记录日志表示自动配置成功。
*/
init {
printBanner()
log.info("MingliUtils AutoConfiguration succeed")
log.info("MingliUtils AutoConfigurationBean succeed")
}
}
@@ -1,91 +0,0 @@
/*
* Copyright 2025 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 GsonAutoConfiguration.kt
* LastUpdate 2025-09-15 10:29:30
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.springboot.autoconfigure
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.mingliqiye.utils.json.GsonJsonApi
import com.mingliqiye.utils.json.JsonApi
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter
import com.mingliqiye.utils.json.converters.JsonStringConverter
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter
import com.mingliqiye.utils.logger.mingLiLoggerFactory
import com.mingliqiye.utils.time.DateTime
import com.mingliqiye.utils.uuid.UUID
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.AutoConfigureAfter
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.gson.GsonBuilderCustomizer
import org.springframework.context.annotation.Bean
@ConditionalOnClass(Gson::class)
@AutoConfiguration
@AutoConfigureAfter(
name = ["org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration",
"com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration"]
)
open class GsonAutoConfiguration {
companion object {
private val log: Logger = LoggerFactory.getLogger("MingliUtils-GsonAutoConfiguration")
fun addTypeAdapter(gsonBuilder: GsonBuilder): GsonBuilder {
val dateTimeJsonConverter: JsonStringConverter<DateTime> = DateTimeJsonConverter()
val uuidJsonStringConverter: JsonStringConverter<UUID> = UUIDJsonStringConverter()
try {
return gsonBuilder
.registerTypeAdapter(
uuidJsonStringConverter.tClass,
uuidJsonStringConverter
.gsonJsonStringConverterAdapter
.gsonTypeAdapter
)
.registerTypeAdapter(
dateTimeJsonConverter.tClass,
dateTimeJsonConverter
.gsonJsonStringConverterAdapter
.gsonTypeAdapter
)
} finally {
log.info("MingliUtils GsonBuilder TypeAdapter add")
}
}
}
private val log: Logger = mingLiLoggerFactory.getLogger("MingliUtils-GsonAutoConfiguration")
@Bean
open fun mingliGsonCustomizer(): GsonBuilderCustomizer {
return GsonBuilderCustomizer { gsonBuilder: GsonBuilder -> addTypeAdapter(gsonBuilder) }
}
@Bean
@ConditionalOnMissingBean
open fun jsonApi(gson: Gson): JsonApi {
log.info("MingliUtils-JsonApiAutoConfiguration: GsonJsonApi bean is created.")
return GsonJsonApi(gson)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2025 mingliqiye
* 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.
@@ -16,61 +16,91 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile JacksonAutoConfiguration.kt
* LastUpdate 2025-09-15 10:29:02
* LastUpdate 2026-02-05 10:45:19
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.springboot.autoconfigure
import com.fasterxml.jackson.databind.ObjectMapper
import com.mingliqiye.utils.json.JacksonJsonApi
import com.mingliqiye.utils.json.JsonApi
import com.mingliqiye.utils.json.api.JSONA
import com.mingliqiye.utils.json.api.JacksonJsonApi
import com.mingliqiye.utils.json.api.base.JsonApi
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter
import com.mingliqiye.utils.json.converters.UUIDJsonStringConverter
import com.mingliqiye.utils.logger.mingLiLoggerFactory
import com.mingliqiye.utils.json.converters.UUIDJsonConverter
import com.mingliqiye.utils.json.converters.base.registerModule
import com.mingliqiye.utils.logger.MingLiLoggerFactory
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.AutoConfigureAfter
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Primary
@ConditionalOnClass(ObjectMapper::class)
@AutoConfiguration
@AutoConfigureAfter(org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration::class)
/*
* 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 JacksonAutoConfiguration.kt
* LastUpdate 2026-02-05 10:39:54
* UpdateUser MingLiPro
*/
/*
* JacksonAutoConfiguration 是一个 Spring Boot 自动配置类,用于配置 Jackson 相关的序列化和反序列化功能。
* 该类在检测到 ObjectMapper 和 Spring Boot 的 JacksonAutoConfiguration 存在时自动生效,
* 并注册自定义的 JSON 转换器模块(如 UUID 和 DateTime 转换器)。
*
* @param objectMapper 用于 JSON 序列化和反序列化的 ObjectMapper 实例。
*/
@ConditionalOnClass(name = ["com.fasterxml.jackson.databind.ObjectMapper", "org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
@AutoConfigureAfter(name = ["org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
open class JacksonAutoConfiguration(objectMapper: ObjectMapper) {
companion object {
private val log: Logger = mingLiLoggerFactory.getLogger("MingliUtils-JacksonAutoConfiguration")
fun addModules(objectMapper: ObjectMapper): ObjectMapper {
return objectMapper
.registerModule(
DateTimeJsonConverter()
.jacksonJsonStringConverterAdapter
.jacksonModule
)
.registerModule(
UUIDJsonStringConverter()
.jacksonJsonStringConverterAdapter
.jacksonModule
)
}
}
private val log: Logger = LoggerFactory.getLogger("MingliUtils-JacksonAutoConfiguration")
private val log: Logger = MingLiLoggerFactory.getLogger("MingliUtils-JacksonAutoConfiguration")
/*
* 初始化块:在类实例化时执行。
* 注册自定义的 UUID 和 DateTime JSON 转换器模块到 ObjectMapper 中。
*/
init {
addModules(objectMapper)
log.info("MingliUtils Jackson Serializers created")
objectMapper
.registerModule<UUIDJsonConverter>()
.registerModule<DateTimeJsonConverter>()
}
/*
* 创建并返回一个 JsonApi Bean 实例。
* 该方法会在没有其他 JsonApi Bean 存在时被调用,并将创建的 JacksonJsonApi 实例设置为全局默认的 JSON API。
*
* @param objectMapper 用于 JSON 操作的 ObjectMapper 实例。
* @return 返回配置好的 JsonApi 实例。
*/
@Bean
@Primary
@ConditionalOnMissingBean
open fun jsonApi(objectMapper: ObjectMapper): JsonApi {
log.info("MingliUtils-JsonApiAutoConfiguration: JacksonJsonApi bean is created.")
return JacksonJsonApi(objectMapper)
return JacksonJsonApi(objectMapper).also {
try {
JSONA.getJsonApi()
} catch (_: NullPointerException) {
JSONA.setJsonApi(it)
log.info("JSONA Use {}", it.javaClass.name)
}
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2025 mingliqiye
* 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.
@@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile SpringBeanUtils.kt
* LastUpdate 2025-09-19 20:07:08
* LastUpdate 2026-02-04 16:50:39
* UpdateUser MingLiPro
*/
@@ -39,6 +39,9 @@ class SpringBeanUtils : ApplicationContextAware {
@JvmStatic
private var applicationContext: ApplicationContext? = null
@Throws(BeansException::class)
inline fun <reified T> getBean() = getBean(T::class.java)
/**
* 根据Bean名称获取Bean实例
*
@@ -48,11 +51,9 @@ class SpringBeanUtils : ApplicationContextAware {
* @throws ClassCastException 当类型转换失败时抛出
*/
@JvmStatic
@Throws(BeansException::class, ClassCastException::class)
@Throws(BeansException::class)
@Suppress("UNCHECKED_CAST")
fun <T> getBean(name: String): T {
return applicationContext!!.getBean(name) as T
}
fun getBean(name: String): Any = applicationContext!!.getBean(name)
/**
* 根据Bean类型获取Bean实例
@@ -0,0 +1,35 @@
/*
* 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 DateTimeToStringConverter.kt
* LastUpdate 2026-02-04 21:57:43
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.springboot.converters
import com.mingliqiye.utils.time.DateTime
import com.mingliqiye.utils.time.Formatter
import org.springframework.core.convert.converter.Converter
import org.springframework.stereotype.Component
@Component
class DateTimeToStringConverter : Converter<DateTime, String?> {
override fun convert(source: DateTime): String {
return source.format(Formatter.STANDARD_DATETIME)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2025 mingliqiye
* 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.
@@ -15,8 +15,8 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Converters.kt
* LastUpdate 2025-09-15 09:19:48
* CurrentFile StringToDateTimeConverter.kt
* LastUpdate 2026-02-04 21:57:43
* UpdateUser MingLiPro
*/
@@ -24,35 +24,12 @@ package com.mingliqiye.utils.springboot.converters
import com.mingliqiye.utils.time.DateTime
import com.mingliqiye.utils.time.Formatter
import com.mingliqiye.utils.uuid.UUID
import com.mingliqiye.utils.uuid.UUID.Companion.of
import org.springframework.core.convert.converter.Converter
import org.springframework.stereotype.Component
@Component
class DateTimeToStringConverter : Converter<DateTime, String?> {
override fun convert(source: DateTime): String {
return source.format(Formatter.STANDARD_DATETIME)
}
}
@Component
class UUIDToStringConverter : Converter<UUID, String> {
override fun convert(source: UUID): String {
return source.getString()
}
}
@Component
class StringToDateTimeConverter : Converter<String, DateTime> {
override fun convert(source: String): DateTime {
return DateTime.parse(source, Formatter.STANDARD_DATETIME_MILLISECOUND7, true)
}
}
@Component
class StringToUUIDConverter : Converter<String, UUID> {
override fun convert(source: String): UUID {
return of(source)
}
}
@@ -0,0 +1,34 @@
/*
* 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 StringToUUIDConverter.kt
* LastUpdate 2026-02-04 21:58:01
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.springboot.converters
import com.mingliqiye.utils.uuid.UUID
import org.springframework.core.convert.converter.Converter
import org.springframework.stereotype.Component
@Component
class StringToUUIDConverter : Converter<String, UUID> {
override fun convert(source: String): UUID {
return UUID.of(source)
}
}
@@ -0,0 +1,34 @@
/*
* 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 UUIDToStringConverter.kt
* LastUpdate 2026-02-04 21:57:43
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.springboot.converters
import com.mingliqiye.utils.uuid.UUID
import org.springframework.core.convert.converter.Converter
import org.springframework.stereotype.Component
@Component
class UUIDToStringConverter : Converter<UUID, String> {
override fun convert(source: UUID): String {
return source.getString()
}
}