no message
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
-91
@@ -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)
|
||||
}
|
||||
}
|
||||
+62
-32
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user