In-depth understanding of the NANNING framework technical principles in the Java class library
In -depth understanding of the nanning framework technical principles in the Java class library
Overview:
Nanning is a lightweight Java framework that is specially used for AOP (facing cut -faced programming) and dynamic proxy.It provides a scalable mechanism to enhance the function of the Java class without modifying its source code.This article will introduce the technical principles of the NANNING framework in detail, including the basic concepts of AOP, the implementation of dynamic proxy, and the core components and configurations in the NANNING framework.
1. AOP basic concept:
1.1 cut surface (aspect):
The cut surface refers to the modular packaging of some horizontal sectaries generated during the process of the program execution.For example, log records, transaction management, etc. can be implemented as cut surfaces.
1.2 Joinpoint:
The connection point is some specific points in the program execution process, such as method calls, abnormal processing, etc.
1.3 PointCut:
The entry point is the set of the connection point. It defines a set of connection points and matches these connection points by specified conditions.
1.4 Notification (Advice):
The notification defines the code logic to be executed during the execution of the entry point, including front notice, rear notification, abnormal notification, etc.
1.5 target object:
The target object is an enhanced object. It exists in the entry point and will be notified to be knitted into the cut surface.
2. Dynamic proxy implementation method:
2.1 JDK dynamic proxy:
The JDK dynamic proxy is based on interface -based proxy, which requires the target object to implement one or more interfaces.Through the cooperation between the Proxy class and the InvacationHandler interface, the proxy class can be created dynamically at runtime, and the method of the target object is entrusted to the Invocationhandler to handle it.
2.2 CGLIB dynamic proxy:
CGLIB dynamic proxy is an agent achieved by inheriting the target class.It will dynamically create a subclass of the target class and rewrite the method, adding the enhancement logic to the rewriting method.
3. NANNING framework core component and configuration:
3.1 ASPECT Configuration:
The information of the cut surface is defined by configuring files or annotations, including entry points, notifications, etc.
3.2 Proxy Factory:
The proxy factory is responsible for creating an agent object and associating the target object with the cut surface.It uses the technical implementation of the JDK dynamic agent and the CGLIB dynamic proxy, and selects the appropriate proxy method according to the type of target object.
3.3 Enhancer:
The enhancer is the core component of the NANNING framework, which is responsible for enhancing the target object method during runtime.The enhancer applies the notification of the cut surface to the method of the target object, and determines whether to weave the notification according to the cut point conditions.
3.4 Advisor链(Advisor Chain):
Advisor chain is an orderly sequence of cutting surface, and the notification in the cut surface is executed in order in the order of definition.Nanning uses the responsibility chain mode to implement the Advisor chain.
3.5 Configuration file:
Nanning's configuration file is used to define the information of the cut surface, the way agent, etc.The configuration file can use the XML format or annotation method, and select the appropriate configuration method as needed.
4. Example code and configuration:
Here are a sample code that uses the NANNNG framework to implement AOP:
import org.codehaus.nanning.*;
import org.codehaus.nanning.config.AspectSystem;
public class Example {
public static void main(String[] args) {
// Create the target object
TargetObject targetObject = new TargetObject();
// Create cut surface
Aspect aspect = new Aspect();
// Define the entry point and notification
Pointcut pointcut = new Pointcut("execution(* TargetObject.someMethod(..))");
Advised advised = new Advised(pointcut, aspect);
// Create an AOP system
AspectSystem aspectSystem = new AspectSystem();
aspectSystem.setProxyFactory(new ProxyFactory());
aspectSystem.register(advised);
// Get the proxy object
TargetObject proxy = (TargetObject) aspectSystem.getProxyFactory().createProxy(targetObject);
// The method of calling the proxy object
proxy.someMethod();
}
}
public class TargetObject {
public void someMethod() {
System.out.println ("Performing Target Method");
}
}
In the above code, a target targetObject is created, and a starting point and notification are defined.Then, a AOP system was created and the target object was associated with the cut surface.Finally, obtain an agent object through the AOP system's proxy factories and call its method.
The configuration file of the Nanning framework can be used in the XML format. The example is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<aspect-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://nanning.codehaus.org/xml/xsd/aspect-config_1_0.xsd
http://nanning.codehaus.org/xml/xsd/aspect-config_1_0.xsd">
<aspect pointcut="execution(* TargetObject.someMethod(..))">
<advice class="org.codehaus.nanning.samples.LoggingAdvice" when="before"/>
<advice class="org.codehaus.nanning.samples.TransactionAdvice" when="after"/>
</aspect>
</aspect-config>
In the above configuration file, a cut surface is defined to perform log records and transaction processing of the tarmethod method of the target object.
in conclusion:
The Nanning framework is a powerful and flexible AOP framework that can enhance the Java class by dynamic agency.This article introduces the basic concepts of AOP, the implementation of dynamic agents, and the core components and configurations in the Nanning framework.It is hoped that through the technical principles of in -depth understanding of the NANNING framework, readers can better use and expand the framework to achieve more flexible and maintainable Java applications.