preface
Previously read the Spring Boot source listener mode, more or less a little harvest it.
Read what Spring Boot has done to the listener mode
Consolidate knowledge of listener patterns
As part of the pre-knowledge to explore the source code of Spring Boot listener, in the “in-depth study of Spring Boot” (3) Spring Boot listener mode (1) “, the first implementation of a simple listener mode, as we continue to explore the cornerstone.
You learned about the extended use of listeners
For example, you can wrap the event class by sending an event to the broadcaster.
Let’s continue with the simple demo we wrote earlier:
// Broadcast wrapper class
@Component
public class DailyLifeRunListener {
@Autowired
private DailyLifeEventMulticaster eventMulticaster;
public void snow(a) {
eventMulticaster.multicastEvent(new FangPiEvent());
}
public void rain(a) {
eventMulticaster.multicastEvent(newLaShiEvent()); }}Copy the code
Listener events can be identified by generics
When a listener needs to listen for only one event, consider using generics to represent the event being listened for.
-
Retrofit listener
public interface DailyLiferListener<T extends DailyLlifeEvent> { void onDailyLlifeEvent(DailyLlifeEvent event); } Copy the code
-
Reform the poop incident
@Component public class LaShiListener implements DailyLiferListener<LaShiEvent> { @Override public void onDailyLlifeEvent(DailyLlifeEvent event) { ResolvableType generic = ResolvableType.forClass(this.getClass()).as(DailyLiferListener.class).getGeneric(); if (generic.isAssignableFrom(LaShiEvent.class)) { System.out.println("Septic Tank attention: You"+event.getBehavior()+"The"); }else { System.out.println("Events of no interest have been skipped."); }}}Copy the code
ResolvableType
This is a listener independent class. But it is widely used in Spring Boot listener mode.
ResolvableType is a utility class that Spring wraps to simplify the extraction of generic information.
public class ResolvableType implements Serializable {
// Create ResolvableType based on the original Class type
public static ResolvableType forClass(@NullableClass<? > clazz);
// Create ResolvableType based on the constructor argument
public static ResolvableType forConstructorParameter(Constructor<? > constructor,int parameterIndex);
// Create ResolvableType based on the member variable
public static ResolvableType forField(Field field);
// Create ResolvableType based on the instance
public static ResolvableType forInstance(Object instance);
// Create ResolvableType based on the method arguments
public static ResolvableType forMethodParameter(Method method, int parameterIndex);
// Create ResolvableType based on the return value of the method
public static ResolvableType forMethodReturnType(Method method);
// Create ResolvableType based on the original type information
public static ResolvableType forRawClass(@NullableClass<? > clazz);
// Create a ResolvableType based on a type
public static ResolvableType forType(@Nullable Type type);
}
Copy the code
This is the static method that constructs a ResolvableType.
To understand the simplicity of ResolvalbeType, we use a set of methods for obtaining generics of generics:
public class RelolvableTypeExample {
private Map<String, Map<Long, List<Integer>>> param;
public static void main(String[] args) throws NoSuchFieldException {
ResolvableType resolvableType = ResolvableType.forField(RelolvableTypeExample.class.getDeclaredField("param"));
// Get generic strings in Map
>>
,>
ResolvableType generic = resolvableType.getGeneric(0);
System.out.println(generic.resolve()); // class java.lang.String
Map
>
,>
ResolvableType generic1 = resolvableType.getGeneric(1); // interface java.util.Map
System.out.println(generic1.resolve());
// Get the generic Map
> generic Long
,>
ResolvableType generic2 = generic1.getGeneric(0);// class java.lang.Long
System.out.println(generic2.resolve());
Map
> List
,>
ResolvableType generic3 = generic1.getGeneric(1);// interface java.util.List
System.out.println(generic3.resolve());
// Get the generic Integer of List
ResolvableType generic4 = generic3.getGeneric(0);// class java.lang.IntegerSystem.out.println(generic4.resolve()); }}Copy the code