当前位置:网站首页>序列化、监听、自定义注解
序列化、监听、自定义注解
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();
}
}
}
边栏推荐
- factory type_ Id:: create process resolution
- [MFC development (16)] tree control
- 足球篮球体育比赛比分直播平台源码/app开发建设项目
- Shell script - definition, assignment and deletion of variables
- Nacos - 服务发现
- Shell script - special variables: shell $, $*, [email protected], $$$
- NIO-零拷贝
- 【MFC开发(17)】高级列表控件List Control
- [MFC development (17)] advanced list control list control
- Nacos - gestion de la configuration
猜你喜欢

为什么LTD独立站就是Web3.0网站!

Audio audiorecord create (I)

一文纵览主流 NFT 市场平台版税、服务费设计

Nacos - 配置管理

Personal decoration notes
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

NIO-零拷贝

安装Oracle EE

Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling

MD文档中插入数学公式,Typora中插入数学公式
随机推荐
Redis源码学习(29),压缩列表学习,ziplist.c(二)
Shell script case in statement
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Pain points and solutions of fixed assets management of group companies
19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
Shell脚本-if else语句
又到年中,固定资产管理该何去何从?
R语言观察日志(part24)--初始化设置
In depth learning training sample amplification and tag name modification
Nacos - 配置管理
3、Modbus通讯协议详解
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
Shell脚本-字符串
C语言学生信息管理系统
挖财打新股安全吗
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
Bimianhongfu queren()
固定资产管理系统让企业动态掌握资产情况
How can enterprises and developers take the lead in the outbreak of cloud native landing?
Shell脚本-数组定义以及获取数组元素