当前位置:网站首页>Serialization, listening, custom annotation
Serialization, listening, custom annotation
2022-07-01 08:54:00 【The most beautiful programmer】
- Separate use jdk,protobuf,json Serialization deserialization of an object of a specific class
// jdk
// serialize
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\Person.txt"));
Person person = new Person();
// Call the method that writes the object writeObject
oos.writeObject(person);
oos.flush();
oos.close();
// Deserialization
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
// serialize
DataInfo.Student student = DataInfo.Student.newBuilder()
.setName("xxxx").setAge(100).setAddress(" China ").build();
byte[] bytes = student.toByteArray();
// Deserialization
DataInfo.Student student1 = DataInfo.Student.parseFrom(bytes);
// take Java Object serialization to Json character string
String objectToJson = JSON.toJSONString(initUser());
System.out.println(objectToJson);
// take Json Deserialize string to Java object
User user = JSON.parseObject(objectToJson, User.class);
System.out.println(user);
- be based on guava Event bus in the framework @Listener Annotations realize the monitoring of specific events
@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();
}
}
}
- Recognize when the application starts spring bean The instance is marked with @Counter Methods
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 {
/** * Task a : Read parsing xml, Instantiate objects by reflection and store them for use (map aggregate ) * Task 2 : It provides an interface for getting instance objects ( according to id obtain ) */
private static Map<String, Object> map = new HashMap<>(); // Store the object
static {
try {
// Task a 、 Scanning package , Instantiate objects by reflection and store them for use (map aggregate )
// Through reflection technology , Scan the package and get a collection of reflection objects
Reflections edus = new Reflections("com.lagou.edu");
Set<Class<?>> clazzs = edus.getTypesAnnotatedWith(Counter.class);
// Traversing the collection of objects
for (Class<?> clazz : clazzs) {
// Get the instanced object
Object object = clazz.getDeclaredConstructor().newInstance();
Counter service = clazz.getAnnotation(Counter.class);
// Judge Service Whether there is a custom object on the annotation ID
if (StringUtils.isEmpty(service.value())) {
// because getName Get the fully qualified class name , So we need to split and remove the previous package name
String[] names = clazz.getName().split("\\.");
map.put(names[names.length - 1], object);
} else {
map.put(service.value(), object);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
边栏推荐
- It is designed with high bandwidth, which is almost processed into an open circuit?
- 易点易动助力企业设备高效管理,提升设备利用率
- Promise异步编程
- 5mo3 UHI HII HII 17mn4 19Mn6 executive standard
- Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
- Shell脚本-case in 和正则表达式
- 固定资产管理系统让企业动态掌握资产情况
- pcl_viewer命令
- Embedded Engineer Interview frequently asked questions
- [MFC development (17)] advanced list control list control
猜你喜欢

Bimianhongfu queren()

Nacos - Configuration Management

截图小妙招

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

jeecg 重启报40001

Glitch Free时钟切换技术

Model and view of QT

MD文档中插入数学公式,Typora中插入数学公式

1. Connection between Jetson and camera

How to solve the problem of fixed assets management and inventory?
随机推荐
MySQL8.0学习记录17 -Create Table
Serial port to WiFi module communication
Shell脚本-数组定义以及获取数组元素
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
Interrupt sharing variables with other functions and protection of critical resources
基础:2.图像的本质
AVL树的理解和实现
Configuration and startup of Chang'an chain synchronization node
Glitch Free时钟切换技术
我想知道手机注册股票开户的流程?另外,手机开户安全么?
Share 7 books I read in the first half of 2022
Nacos - 服务发现
【MFC开发(17)】高级列表控件List Control
MD文档中插入数学公式,Typora中插入数学公式
Shell script - special variables: shell $, $*, [email protected], $$$
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
避免按钮重复点击的小工具bimianchongfu.queren()
3. Detailed explanation of Modbus communication protocol
Advanced C language pointer (Part 2)
Shell script echo command escape character