栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

序列化、监听、自定义注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

序列化、监听、自定义注解

  1. 分别使用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);  
  1. 基于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();
        }
    }
}
  1. 应用启动时识别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 {

    
    private static Map map = new HashMap<>();  // 存储对象

    static {
        try {
            //任务一、扫描包,通过反射技术实例化对象并且存储待用(map集合)

            //通过反射技术,扫描包并获取反射对象集合
            Reflections edus = new Reflections("com.lagou.edu");
            Set> 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();
        }
    }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/959665.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号