In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the concept of AOP in Spring and the implementation of JDK dynamic agent". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the concept of AOP in Spring and the implementation of JDK dynamic agent".
The concept of AOP
AOP:Aspect-Oriented Programming (aspect-oriented programming), Wikipedia explains as follows: Aspect is a new modular mechanism used to describe crosscutting concerns scattered in objects, classes, or functions. Separating crosscutting concerns from concerns is the core concept of aspect-oriented programming. The separation of concerns makes the code to solve specific domain problems independent from the business logic, and the code of business logic no longer contains calls to specific domain problems. the relationship between business logic and domain-specific problems is encapsulated and maintained through aspects, so that changes that are originally scattered in the whole application can be well managed. From the perspective of AOP, applications can be divided into crosscutting concerns and business logic codes. In actual development, these crosscutting concerns are often directly embedded in business logic codes. Aspect-oriented programming is to separate crosscutting concerns from business logic.
Implementation method:
Spring uses the JDK dynamic proxy as the AOP proxy by default, but the drawback is that the class of the target class must implement the interface, otherwise the JDK dynamic proxy cannot be used. If you need a class instead of an interface, Spring will use the CGLIB proxy by default. About the difference between the two: the jdk dynamic proxy is implemented through the reflection mechanism of java, the target class must implement the interface, and cglib implements the proxy for the class. His principle is to dynamically generate a subclass for the specified target class and override the method to implement the enhancement, but because it adopts inheritance Therefore, final-modified classes cannot be proxied.
JDK dynamic agent
Jdk dynamic proxy dynamically generates the class file of the agent class according to the interface implemented by the target class during the running of the program, which mainly involves two classes:
InvocationHandler interface: it provides an invoke (Object obj,Method method, Object [] args) method for implementers to provide the corresponding proxy logic implementation. You can do some special processing for the actual implementation, in which the parameters
Object obj: the target class of the delegate
Method method: methods of the target class that need to be executed
Object [] args: parameters of the target method
Proxy class: provides a method newProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h) to get the dynamic proxy class
Sample code:
Public interface OrderService {public void createOrder ();} public class OrderServiceImpl implements OrderService {@ Override public void createOrder () {System.out.println ("creating order");}} public class OrderLogger {public void beforeCreateOrder () {System.out.println ("before create order");} public void afterCreateOrder () {System.out.println ("after create order");}} package com.sl.aop;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy Public class ServiceProxy implements InvocationHandler {private Object targetClass; private OrderLogger orderLogger; public ServiceProxy (Object targetClass,OrderLogger orderLogger) {this.targetClass = targetClass; this.orderLogger = orderLogger;} / / get proxy public Object GetDynamicProxy () {return Proxy.newProxyInstance (targetClass.getClass () .getClassLoader (), / / generate proxy object targetClass.getClass () .getInterfaces (), / / interface this implemented by proxy class through this ClassLoader) / / the dynamic proxy invocation method is the associated InvocationHandler, and the real method} / / implements the corresponding proxy logic @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {this.orderLogger.beforeCreateOrder (); Object result= method.invoke (targetClass, args); this.orderLogger.afterCreateOrder (); return result;}} through the invoke method of this proxy
Test class:
Package com.sl.aop;import org.junit.Test;public class AopTest {@ Test public void Testdynamicproxy () {OrderServiceImpl serviceImpl = new OrderServiceImpl (); OrderLogger logger = new OrderLogger (); OrderService service = (OrderService) new ServiceProxy (serviceImpl, logger). GetDynamicProxy (); service.createOrder ();}}
Running result:
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.