Event handling in the ApplicationContext is provided through the ApplicationEvent class and the ApplicationListener interface. If a bean that implements the ApplicationListener interface is deployed into the context, every time an ApplicationEvent gets published to the ApplicationContext, that bean is notified. Essentially, this is the standard Observer design pattern.
// 官方文档描述当监听多个事件时,不允许定义任何参数 但是实际测试可以定义一个参数 // The event classes that this listener handles. // <p>If this attribute is specified with a single value, the // annotated method may optionally accept a single parameter. // However, if this attribute is specified with multiple values, // the annotated method must <em>not</em> declare any parameters. @EventListener(classes = {PaymentEvent.class, EmailEvent.class}) publicvoidprocessBlockedListEvent(ApplicationEvent event){ if (event instanceof PaymentEvent) { System.out.println("接收到事件:" + ((PaymentEvent) event).getAccount()); }
If an asynchronous event listener throws an Exception, it is not propagated to the caller. See AsyncUncaughtExceptionHandler for more details.
不支持事件传播
Asynchronous event listener methods cannot publish a subsequent event by returning a value. If you need to publish another event as the result of the processing, inject an ApplicationEventPublisher to publish the event manually.
As of Spring 4.2, the event infrastructure has been significantly improved and offers an annotation-based model as well as the ability to publish any arbitrary event (that is, an object that does not necessarily extend from ApplicationEvent). When such an object is published, we wrap it in an event for you.
Spring4.2 之后,可以不继承 ApplicationEvent,直接以 model 形式实现事件.
ObjectMapper mapper = new ObjectMapper(); Car car = new Car(); car.setName("A8L"); car.setBand("Audi"); car.setBirth(new Date()); final String value = mapper.writeValueAsString(car);
logger.info(value); } // 10:37:37.557 [Test worker] INFO zlin.site.framework.JackjsonTest - {"name":"A8L","birth":"2021-07-24"}
对象反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
@Test publicvoidreadJsonAsClassInstance()throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000}"; final Car car = mapper.readValue(json, Car.class); logger.info(car.band);
final JsonNode readTree = mapper.readTree(json); final String name = readTree.get("name").asText(); logger.info("name: {}", name);
// 凡序列化一个对象的属性为对象这种情况 String jsonHasObjectProperties = "{\"name\":\"A8L\",\"birth\":\"2021-07-24\",\"price\":900000,\"color\":{\"name\":\"read\"}}"; final JsonNode jsonNode = mapper.readTree(jsonHasObjectProperties); final String color = jsonNode.get("color").get("name").asText(); logger.info("color={}",color); }
String jsonArray = "[{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000},{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000}]"; List<Car> list = mapper.readValue(jsonArray, List.class); for (Car car : list) { logger.info("name:{}", car.getBand()); } } // java.util.LinkedHashMap cannot be cast to zlin.site.framework.JackjsonTest$Car // java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to zlin.site.framework.JackjsonTest$Car
@Test publicvoidcustomizeSerializer()throws JsonProcessingException { Person person = new Person(); person.setUsername("小汪"); person.setGender("女"); person.setAge(19); ObjectMapper mapper = new ObjectMapper();
final String value = mapper.writeValueAsString(person); logger.info("Person={}", value); } // 13:17:56.356 [Test worker] INFO zlin.site.framework.JackjsonTest - Person={"user-name":"小汪","age":39}