当前位置:网站首页>序列化、监听、自定义注解
序列化、监听、自定义注解
2022-07-01 08:52:00 【程序员中最靓的仔】
- 分别使用jdk,protobuf,json序列化反序列化一个特定类的对象
// jdk
// 序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\Person.txt"));
Person person = new Person();
//调用写对象的方法writeObject
oos.writeObject(person);
oos.flush();
oos.close();
// 反序列化
FileInputStream fis = new FileInputStream("E:\\Person.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
System.out.println(o);
fis.close();
ois.close();
//protobuf
// 序列化
DataInfo.Student student = DataInfo.Student.newBuilder()
.setName("xxxx").setAge(100).setAddress("中国").build();
byte[] bytes = student.toByteArray();
// 反序列化
DataInfo.Student student1 = DataInfo.Student.parseFrom(bytes);
// 将Java对象序列化为Json字符串
String objectToJson = JSON.toJSONString(initUser());
System.out.println(objectToJson);
// 将Json字符串反序列化为Java对象
User user = JSON.parseObject(objectToJson, User.class);
System.out.println(user);
- 基于guava框架中事件总线@Listener注解实现具体事件的监听
@Service
public class EventListener {
@org.springframework.context.event.EventListener
public void onSynMsgEvent(CreateMsgEvent event){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Async
@org.springframework.context.event.EventListener
public void onAsynMsgEvent(CreateMsgEvent event){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 应用启动时识别spring bean实例中标记有@Counter的方法
import java.lang.annotation.*;
@Target({
ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Counter{
boolean value() default true;
}
public class BeanFactory {
/** * 任务一:读取解析xml,通过反射技术实例化对象并且存储待用(map集合) * 任务二:对外提供获取实例对象的接口(根据id获取) */
private static Map<String, Object> map = new HashMap<>(); // 存储对象
static {
try {
//任务一、扫描包,通过反射技术实例化对象并且存储待用(map集合)
//通过反射技术,扫描包并获取反射对象集合
Reflections edus = new Reflections("com.lagou.edu");
Set<Class<?>> clazzs = edus.getTypesAnnotatedWith(Counter.class);
//遍历对象集合
for (Class<?> clazz : clazzs) {
// 获取实例化对象
Object object = clazz.getDeclaredConstructor().newInstance();
Counter service = clazz.getAnnotation(Counter.class);
//判断Service注解上是否有自定义对象ID
if (StringUtils.isEmpty(service.value())) {
//由于getName获取的是全限定类名,所以要分割去掉前面包名部分
String[] names = clazz.getName().split("\\.");
map.put(names[names.length - 1], object);
} else {
map.put(service.value(), object);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
边栏推荐
猜你喜欢

Ape anthropology topic 20 (the topic will be updated from time to time)

Installing Oracle EE

Glitch Free时钟切换技术

TV size and viewing distance
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

FreeRTOS learning easy notes

Matlab tips (23) matrix analysis -- simulated annealing

Only in China! Alicloud container service enters the Forrester leader quadrant

factory type_ Id:: create process resolution

ARM v7的体系结构A、R、M区别,分别应用在什么领域?
随机推荐
Differences among tasks, threads and processes
Computer tips
Bimianhongfu queren()
It technology ebook collection
Glitch free clock switching technology
1. Connection between Jetson and camera
用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
【MFC开发(17)】高级列表控件List Control
MySQL8.0学习记录17 -Create Table
Do you know how data is stored? (C integer and floating point)
《微机原理》-绪论
C语言指针的进阶(下)
Shell script - special variables: shell $, $*, [email protected], $$$
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
IT 技术电子书 收藏
Memory size end
基础:3.opencv快速入门图像和视频
Glitch Free时钟切换技术
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
Shell脚本-echo命令 转义符