In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the usage of pre-enhancement in Spring framework". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the usage of pre-enhancement in Spring framework.
In the previous article, we have implemented simple AOP functions through dynamic proxy technology, but if we look closely, we will find that there are several obvious areas that need to be improved in the way we implemented in the previous article, they are:
All the methods of the target class add thing management, and sometimes we just need to add things to special methods. For example, in the example in the previous article, we only need to add things to the write operation, that is, the register method, but not to read things, that is, the login method.
We specify the logic of crosscutting through a hard-coded method, that is, adding code before and after the start of the target method.
We manually created the proxy class, so that when we create the proxy for different classes, we need to write the corresponding code separately, which cannot be used in general.
To solve the above problems, the solution is to use the enhancements provided by spring. In other articles, we have introduced that enhancement is the information added to the special join points of the target class, and the enhancements provided to us by spring are all with orientation information, that is, before and after the method. So we can easily use enhancements to make us need to add things to the method of dynamically adding weaving functions. Next we will demonstrate what enhancements are available for all of us in spring.
Pre-enhancement
As the name implies, it adds crosscutting logic before the target method is executed. Let's demonstrate the specific use of pre-enhancements through the following use cases.
We know that the bottom layer of the AOP function in spring is also through JDK dynamic proxy or using CGLib dynamic proxy, so which technology is used to implement the above code? Don't worry. Let's look at the underlying implementation of the ProxyFactory class. In the ProxyFactory class, we can specify the use of the JDK dynamic proxy by setting the setInterfaces method. If you are proxying a class, ProxyFactory uses the CGLib dynamic proxy because the JDK proxy does not support it. We can also use the setOptimize method to get ProxyFactory to start the optimization mode so that the CGLib proxy is used even if the interface is proxied.
JDK dynamic agent
CGLib dynamic agent
Let's implement the AOP dynamic proxy function through the sping configuration file.
Let's take a look at the description of the relevant properties of the org.springframework.aop.framework.ProxyFactoryBean class:
Target: the target object of the dynamic agent
ProxyInterfaces: the interface to be implemented by a dynamic proxy. If you are using a CGLib proxy, you can not set this property. You can also set up multiple interfaces at the same time, and when using multiple interfaces, the interfaces and interfaces are separated by commas.
InterceptorNames: a class with and crosscutting logic.
Singleton: whether the proxy object created is a singleton object, the default is singleton object.
Optimize: whether to enable the optimization feature. When the setting is enabled, spring uses CGLib dynamic proxy, while JDK dynamic proxy is used. When set to true, the proxyInterfaces property may not be set.
ProxyTargetClass: whether to proxy the class or not, when set to true, CGLib dynamic proxy is used, and JDK dynamic proxy is used instead.
Thank you for reading, the above is the content of "the usage of pre-enhancements in the Spring framework". After the study of this article, I believe you have a deeper understanding of the usage of pre-enhancements in the Spring framework, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.