Introduction to aspective programming ---- Basic Concept (2)
AOP aspect programming ---- Basic Concept (3)
Aspective programming ideas is simple. From the course-oriented process, the programming to the object-oriented programming to the interface-oriented programming to the component, the history of the module programming. We can know that the evolution of programming methodology is a step-by-step "boundary" that expands programmed. So far, our programming world is "class, interface, or components". In this boundary, we believe that a class implements an interface, which cannot dynamically implement another interface, existing behavior It is basically fixed before compilation or is the method of internal definitions, or it is either inheritance and implementation of the interface inherited. However, in actual programming, we met "cross border", need multiple classes, interfaces, and component cooperation, such as multi-threaded concurrent access, program flow centralized control, serialization, and program status, and There are multiple "classes, interfaces, and component boundaries" to participate in the work that can be completed. In order to better handle multiple borders to complete the work of the same aspect, the facing programming appears. Here, we can refer to the integration of collaborative work together to work together to complete the same task. You can think that it is actually a bigger class. This class is mainly composed of the classes and interfaces in our OOP. Of course, these classes and interfaces are in this intimate, so that they can convert each other. In fact, this is not big, the appearance of the interface is not the behavior of dynamic changing classes? Aspective programming is just an extended extension, and this change is raised to this level. Write here, I feel quiet, but the world is still not quiet, because the actual use of this idea programming, it takes a long time, and more tool developers support.
In my future articles, I want to make a look at the concept: 1. Inter-Type Declarations Border Internal Type Declaration: Inter-Type declaration has many forms in AspectJ, using him to describe the relationship between classes, class itself member and Structural information. 2, join point connection point: The connection point is a point defined in the program process. 4, Crosscutting Concerns: Consider the boundary cross 5, PointCuts connection point set: The set of multiple connection points in the program runs. 5, Advice Notification Point Behavior: The connection point set is like a "structure" that includes multiple "structures", which does not have behavior, and its behavior is implemented by notification point behavior. The above nouns are mainly used in the introduction of aspectj.
JBoss 4.0 provides an AOP framework, which is closely integrated with the JBoss application server, but can also run independently.
Let's take a look at the basic process based on AOP programming:
1. Define "Message Interceptor" Interceptor
JBoss provides a message interceptor interface, as follows:
Public Interface Interceptor, PUBLIC INTERCEPTOR
{
Public string getname ();
Public InvocationResponse Invoke
(
Invocation invocation
)
Throws throwable;
}
The value is: All fields, constructor, etc. in the class are all intercepted by "message interceptor", and then "package" into an Invocation object. After the operation is completed, the IVVocationResponse object is returned.
See the following example:
Import org.jboss.aop. *;
Import java.lang.reflect. *; Public Class TracingInterceptor Implements Interceptor
{
Public string getName ()
{
Return TracingInterceptor;
}
Public InvocationResponse INVOKE
Invocation Invocation)
Throws throwable
{
String message = null;
IF
Invocation.gettype () == InvocationType.method
)
{
Method Method =
MethodInvocation.getMethod (Invocation);
Message =
Method: Method.getName ();
}
ELSE IF
INVOCATION.GETTYPE () ==
InvocationType.constructor
)
{
Constructor c = constructorinvocation.getConstructor (Invocation);
Message = constructor: c.toString ();
}
Else
{
Return invocation.invokenext ();
}
System.out.println (Entering Message);
InvocationResponse RSP = Invocation.Invokenext ();
System.out.println (Leaving Message);
Return RSP;
}
}
2, associate "message interceptors" and a specific class
The association here uses the "glue" PointCut mentioned above, implemented in an XML configuration file:
Like the J2EE configuration file name, the name of the AOP configuration file is fixed: meta-inf / jboss-aop.xml
XML Version = "1.0" encoding = "UTF-8">
interceptors>
interceptor-pointcut>
aop>
The above code is associated with the TracingInterceptor message interceptor and the POJO class.
The code of POJO is as follows:
Public Class Pojo
{
Public pojo () {}
Public void helloworld ()
{System.out.println (Hello World!);
Public static void main (string [] args)
{
Pojo pojo = new pojo ();
Pojo.helloworld ();
}
}
The TracingInterceptor message interceptor will intercept: main (), pojo (), and helloworld () method.
After compiling: Use the console command:
Java -djava.system.class.Loader = Org.jboss.Aop.Standalone.systemclassLoader POJO The result is as follows:
ENTERING METHOD: Main
Entering Construction: Public Pojo ()
Leaving constructor: Public Pojo ()
Entering Method: HelloWorld
Hello World!
Leaving method: HelloWorld
Leaving method: main
How is JBoss to intercepted it?
The role of the AOP frame is as follows:
The center task of the AOP frame is: Message Intercept and Message Agent Delegation.
The AOP framework is specifically completed:
1, component activation (Component Activation)
2, method activation (METHOD Invocation)
Component activation is an object implementation of AOP framework syndrome, and returns a (message interception) Interceptor reference (not returning to object reference). Method activation is the interceptor (message interception) of the Interceptor calls the already registered aspect when a caller call (message interception) Interceptor is interceptor.
The JBoss AOP framework is based on byte code. From implementation, it will be fully controlled by ClassLoader, step by step, and these work JBoss is already done. Of course, if you run outside the JBoss application server, you have to refer to JBoss's Referencer from others to implement ClassLoader.
Strue (herein, other basic functions of AOP)
Accesine@163.com
http://www.onjava.com/pub/a/onjava/2003/05/28/AOP_JBOSS.HTML