In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to achieve callback and spring LambdaSafe class, the article is very detailed, has a certain reference value, interested friends must read it!
When reading the spring boot source code, I found that the WebServerFactoryCustomizerBeanPostProcessor class has
LambdaSafe.callbacks (WebServerFactoryCustomizer.class, getCustomizers (), webServerFactory) .withLogger (WebServerFactoryCustomizerBeanPostProcessor.class) .invoke ((customizer)-> customizer.customize (webServerFactory))
The usage of. I searched the domestic website and found that there was no relevant information, so I could only go to the official website of spring to consult the documents and learn about it.
We know that there are no function pointers in java, where everything can be classed, and function pointers can be used to implement "callbacks" that are more commonly used in programming. The so-called "callback" is the opposite of "calling". If you use classes and messages to understand that class A calls the method of class B, it can be thought that class A sends a message to class B. this process is called "call". For class B, B passively receives this message. However, if the method of class B needs the data of class An or the data of class A to be able to get in the process of processing this message, class B needs to send a message to class A (calling the method of class A). Actively request to obtain data. At this point, class B needs to know how to call the method of class A, a process called "callback".
If it is a class B, you can pass a function pointer to the class A method directly. However, there are no ready-made "function pointers" in java, so there are several types of "function pointers". One is the common way to implement class function pointers in the form of interface + method, which is called functional interface, and the other is the new feature lambda expression in java 8, which is essentially a functional interface.
A @ FunctionalInterface annotation is defined in Java to modify the "function pointer" of the interface implementation, but this annotation is not required, as long as the interface meets the functional interface standard can be used as a "function pointer". If an interface does not meet the functional interface standard and @ FunctionalInterface is annotated, the compiler will report an error, which makes it easier for the compiler to check. Now let's take the functional interface involved in the above code as an example.
@ FunctionalInterfacepublic interface WebServerFactoryCustomizer {void customize (T factory);} WebServerFactoryCustomizer is an interface provided to users to customize WebServerFactory instances. The use of this functional interface is in the lambda expression of the invoke method in the above code, mainly dealing with the bean of the incoming WebServerFactory instance, and then inheriting the functional interface to do whatever you want with the instance. All right, the basic introduction is over, and finally to our protagonist LambdaSafe class. The explanation given by the spring official document is
Utility that can be used to invoke lambdas in a safe way. Primarily designed to help support generically typed callbacks where class cast exceptions need to be dealt with due to class erasure.
. The main idea is that LambdaSafe can be used to run lambda expressions safely, and security means that it can support handling situations that need to be thrown during a "callback" because of type erasure (a concept in java generics).
So how does it actually happen? Let's first look at the LambdaSafe.callbacks method. In the above code, LambdaSafe.callbacks passes in WebServerFactoryCustomizer.class, getCustomizers (), and the WebServerFactory instance that needs to be done as it pleases. The return value of getCustomizers () gets all the implementation classes of the WebServerFactoryCustomizer functional interface, understandably getting a bunch of function pointers.
Private List > getCustomizers () {if (this.customizers = = null) {/ / Look up does not include the parent context this.customizers = new ArrayList (getWebServerFactoryCustomizerBeans ()); this.customizers.sort (AnnotationAwareOrderComparator.INSTANCE); this.customizers = Collections.unmodifiableList (this.customizers);} return this.customizers;}
The callbacks method returns
Public static Callbackscallbacks (Class callbackType, Collection
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.