当前位置:网站首页>序列化、监听、自定义注解
序列化、监听、自定义注解
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();
}
}
}
边栏推荐
猜你喜欢

安装Oracle EE

截图小妙招

Audio audiorecord create (I)

Nacos - 服务发现

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

jeecg 重启报40001

Nacos - service discovery

How to manage fixed assets efficiently in one stop?
![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)
Matlab [function derivation]

It is designed with high bandwidth, which is almost processed into an open circuit?
随机推荐
Daily office consumables management solution
你了解数据是如何存储的吗?(C整型和浮点型两类)
《单片机原理及应用》—定时器、串行通信和中断系统
集团公司固定资产管理的痛点和解决方案
Only in China! Alicloud container service enters the Forrester leader quadrant
Software Engineer Interview Question brushing website and experience method
Nacos - Configuration Management
IT 技术电子书 收藏
Foundation: 2 The essence of image
避免按钮重复点击的小工具bimianchongfu.queren()
MD文档中插入数学公式,Typora中插入数学公式
Introduction to R language
Shell script - special variables: shell $, $*, [email protected], $$$
NFT监管要点和海外政策
Shell script -for loop and for int loop
如何做好固定资产管理?易点易动提供智能化方案
Shell脚本-位置参数(命令行参数)
Shell脚本-for循环和for int循环
Glitch free clock switching technology
Shell script -read command: read data entered from the keyboard