当前位置:网站首页>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();
}
}
}
边栏推荐
猜你喜欢

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

Audio audiorecord create (I)

Jeecg restart alarm 40001

Screenshot tips

ARM v7的体系结构A、R、M区别,分别应用在什么领域?

Embedded Engineer Interview Question 3 Hardware

NIO-零拷贝

AVL树的理解和实现
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

为什么LTD独立站就是Web3.0网站!
随机推荐
1.jetson与摄像头的对接
又到年中,固定资产管理该何去何从?
Shell script - special variables: shell $, $*, [email protected], $$$
我想知道手机注册股票开户的流程?另外,手机开户安全么?
Embedded Engineer Interview Question 3 Hardware
Shell script - array definition and getting array elements
避免按钮重复点击的小工具bimianchongfu.queren()
截图小妙招
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
[MFC development (17)] advanced list control list control
Flink面试题
软件工程师面试刷题网站、经验方法
Daily office consumables management solution
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
Shell script case in and regular expressions
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
如何解决固定资产管理和盘点的难题?
How to manage fixed assets efficiently in one stop?
Principle and application of single chip microcomputer - off chip development
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度