generated from mingliqiye/lib-tem
feat(utils): 添加 Jackson ObjectMapper 支持并优化随机数生成器- 在 AutoConfiguration 中添加对 Jackson ObjectMapper 的支持
All checks were successful
Gitea Actions Build / Build (push) Successful in 51s
All checks were successful
Gitea Actions Build / Build (push) Successful in 51s
- 新增 RandomBytes 和 RandomString 类的方法,增强随机数生成功能 - 优化 FieldStructure 类的字段顺序获取逻辑 - 在 SpringBeanUtil 中修正包名常量
This commit is contained in:
parent
9ebd09a810
commit
0d15cbbcc0
@ -20,7 +20,7 @@ import org.springframework.stereotype.Component;
|
||||
public class SpringBeanUtil implements ApplicationContextAware {
|
||||
|
||||
public static final String PACKAGE_NAME =
|
||||
SpringBeanUtil.class.getPackage().getName();
|
||||
"com.mingliqiye.utils.bean.springboot";
|
||||
|
||||
/**
|
||||
* 获取applicationContext
|
||||
|
@ -13,7 +13,7 @@ public class FieldStructure extends Structure {
|
||||
|
||||
@Override
|
||||
protected List<String> getFieldOrder() {
|
||||
List<String> fieldOrderList = new ArrayList<String>();
|
||||
List<String> fieldOrderList = new ArrayList<>();
|
||||
for (
|
||||
Class<?> cls = getClass();
|
||||
!cls.equals(FieldStructure.class);
|
||||
|
@ -7,22 +7,60 @@ import com.mingliqiye.utils.collection.ForEach;
|
||||
*/
|
||||
public class RandomBytes {
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机字节数组
|
||||
* @param length 数组长度
|
||||
* @return 包含随机字节的数组
|
||||
*/
|
||||
public static byte[] randomBytes(int length) {
|
||||
byte[] bytes = new byte[length];
|
||||
// 使用forEach遍历数组,为每个位置生成随机字节
|
||||
ForEach.forEach(bytes, (b, i) ->
|
||||
bytes[i] = randomByte((byte) 0x00, (byte) 0xff)
|
||||
);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机字节数组
|
||||
* 从给定的字节数组中随机选择字节来填充新数组
|
||||
*
|
||||
* @param length 要生成的随机字节数组的长度
|
||||
* @param bytes 用于随机选择的源字节数组
|
||||
* @return 包含随机字节的新数组
|
||||
*/
|
||||
public static byte[] randomBytes(int length, byte[] bytes) {
|
||||
byte[] rbytes = new byte[length];
|
||||
// 从源数组中随机选择字节填充到结果数组中
|
||||
for (int i = 0; i < length; i++) {
|
||||
rbytes[i] = bytes[RandomInt.randomInt(i, bytes.length - 1)];
|
||||
}
|
||||
return rbytes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成指定范围内的随机字节
|
||||
* @param from 起始字节值(包含)
|
||||
* @param to 结束字节值(包含)
|
||||
* @return 指定范围内的随机字节
|
||||
*/
|
||||
public static byte randomByte(byte from, byte to) {
|
||||
// 将byte转换为int进行计算,避免符号问题
|
||||
int fromInt = from & 0xFF;
|
||||
int toInt = to & 0xFF;
|
||||
int randomValue = RandomInt.randomInt(fromInt, toInt);
|
||||
return (byte) (randomValue & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定范围内的随机字节(不包含边界值)
|
||||
* @param from 起始字节值(不包含)
|
||||
* @param to 结束字节值(不包含)
|
||||
* @return 指定范围内的随机字节
|
||||
*/
|
||||
public static byte randomByteNoHave(byte from, byte to) {
|
||||
// 将byte转换为int进行计算,避免符号问题
|
||||
int fromInt = from & 0xFF;
|
||||
int toInt = to & 0xFF;
|
||||
int randomValue = RandomInt.randomIntNoHave(fromInt, toInt);
|
||||
|
@ -1,13 +1,24 @@
|
||||
package com.mingliqiye.utils.random;
|
||||
|
||||
/**
|
||||
* 随机字符串生成工具类
|
||||
* 提供生成指定长度随机字符串的功能
|
||||
*
|
||||
* @author MingLiPro
|
||||
*/
|
||||
public class RandomString {
|
||||
|
||||
/**
|
||||
* 生成指定长度和字符集的随机字符串
|
||||
*
|
||||
* @param length 要生成的随机字符串长度
|
||||
* @param chars 用于生成随机字符串的字符集
|
||||
* @return 指定长度的随机字符串
|
||||
*/
|
||||
public static String randomString(int length, String chars) {
|
||||
String[] charsd = chars.split("");
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
// 循环生成随机字符并拼接
|
||||
for (int i = 0; i < length; i++) {
|
||||
int index = RandomInt.randomInt(0, charsd.length - 1);
|
||||
sb.append(charsd[index]);
|
||||
@ -15,6 +26,12 @@ public class RandomString {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机字符串,使用默认字符集(数字+大小写字母)
|
||||
*
|
||||
* @param length 要生成的随机字符串长度
|
||||
* @return 指定长度的随机字符串
|
||||
*/
|
||||
public static String randomString(int length) {
|
||||
return randomString(
|
||||
length,
|
||||
|
@ -1,18 +1,26 @@
|
||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.mingliqiye.utils.bean.springboot.SpringBeanUtil;
|
||||
import com.mingliqiye.utils.collection.ForEach;
|
||||
import com.mingliqiye.utils.jackson.Serializers;
|
||||
import com.mingliqiye.utils.time.DateTime;
|
||||
import com.mingliqiye.utils.time.Formatter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author MingLiPro
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(AutoConfiguration.class)
|
||||
@ComponentScan({ SpringBeanUtil.PACKAGE_NAME })
|
||||
public class AutoConfiguration {
|
||||
|
||||
private static String banner =
|
||||
@ -25,13 +33,36 @@ public class AutoConfiguration {
|
||||
"| $$ |\\$ /$$ |$$ | $$ | $$ | $$ | $$\\ $$ | |\n" +
|
||||
"| $$ | \\_/ $$ |$$$$$$$$\\\\$$$$$$ | $$ | \\$$$$$$ | |\n" +
|
||||
"| \\__| \\__|\\________|\\______/ \\__| \\______/ |\n";
|
||||
private boolean isloadObjMapper;
|
||||
|
||||
public AutoConfiguration() throws IOException {
|
||||
print();
|
||||
|
||||
try {
|
||||
Class<?> clasz = ClassLoader.getSystemClassLoader().loadClass(
|
||||
"com.fasterxml.jackson.databind.ObjectMapper"
|
||||
);
|
||||
isloadObjMapper = true;
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
log.info(
|
||||
"Jackson ObjectMapper not found in classpath. Jackson serialization features will be disabled."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new AutoConfiguration();
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (isloadObjMapper) {
|
||||
log.info("init ObjectMapper");
|
||||
Serializers.addSerializers(
|
||||
SpringBeanUtil.getBean(
|
||||
com.fasterxml.jackson.databind.ObjectMapper.class
|
||||
)
|
||||
);
|
||||
log.info("add ObjectMapper Serializers OK");
|
||||
} else {
|
||||
log.info("ObjectMapper is Not Found");
|
||||
}
|
||||
}
|
||||
|
||||
public void print() throws IOException {
|
||||
@ -62,7 +93,6 @@ public class AutoConfiguration {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
System.out.printf(
|
||||
banner,
|
||||
DateTime.now().format(Formatter.STANDARD_DATETIME_MILLISECOUND7)
|
||||
|
Loading…
x
Reference in New Issue
Block a user