当前位置:网站首页>序列化、监听、自定义注解
序列化、监听、自定义注解
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();
}
}
}
边栏推荐
猜你喜欢
随机推荐
Jetson Nano 安装TensorFlow GPU及问题解决
5mo3 UHI HII HII 17mn4 19Mn6 executive standard
猿人学第20题(题目会不定时更新)
IT 技术电子书 收藏
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
Principle and application of single chip microcomputer - principle of parallel IO port
基础:3.opencv快速入门图像和视频
电脑小技巧
Nacos - service discovery
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Redis源码学习(29),压缩列表学习,ziplist.c(二)
In depth learning training sample amplification and tag name modification
1. Connection between Jetson and camera
中小企业固定资产管理办法哪种好?
[MFC development (16)] tree control
Differences among tasks, threads and processes
Embedded Engineer Interview frequently asked questions
Brief introduction to AES
毕业季,我想对你说
NIO-零拷贝









