Programming (AOP) and JBoss (1)

xiaoxiao2021-03-06  59

From: matrix of open source technologies: neo translation [2003/09/27]

Overview Aspect-Orient Programming, AOP is an exciting new mode. For development software systems, its influence will be the same as objects for 15 to 20 years. Aspects of programming and object-oriented programming is not only technologies that compete with each other and can be well complementary. Object-oriented programming is mainly used to model common behavior for the same object level. Its weakness is between the use of public behavior between multiple independent object models. This is precisely a place for AOP. AOP allows define cross-crossed relationships, and those relationships are applied to transnational separation, very different object models. AOP allows you to hierarchize functionality rather than embedding function, which makes code better and easy maintenance. I like to think that OOP is the top-down software development, and AOP is a software development from the right software, and they are completely straightforward technologies and complement each other. In OOP tools are inheritance, packages, and polymorphism, and AOP components are notified / interceptors, introductions, metadata, and Pintcuts. Let's take a look at these definitions. Notification / Interceptor A notification is a logic that triggers this logic. It is a behavior that can be inserted between the caller and the caller, between a method caller and the actual method. The notice is the true key of AOP. Notification allows you to go to transparent application, like logs and logs to an object model that exists. In JBoss AOP, we use interceptors to achieve notifications. You can define interceptors, which intercept method calls, constructor calls, and domain access. Behind, we will clarify how to apply these interceptors to an object model that exists. The introduction introduction is a way to increase methods or domains into a class of existence. They even allow you to change the currently existing classes that are displayed and introduce a mixed class, which is a new interface. The introduction allows you to inherit many Java classes. Introduction One of the main use cases is that when you have an aspect, you want to have a run time excuse. You want to apply your way across different objects, but you still have to apply developers to call specific aspects.

Apple Apple = new apple ();

Loggingapi logging = (loggingapi) apple;

Apple.setloggingLevel (Verbose);

The introduction can be a method that binds a new API to an object model that exists. Metadata metadata is additional information that can be bound to a class, in static or runtime. Metadata is more powerful, you can dynamically bind metadata to a given object instance. The metadata is very powerful. When you really write a general aspect of any object, the logic needs to know the information of the class. A good metadata ratio in use is the EJB specification. In the EJB XML release descriptor, you need to define transaction properties based on each method. When the application server is guided, where it starts, hangs or submits a transaction, because you have defined in the metadata in the XML configuration file in Bean: Required, RequiresNew, Support, etc., they are bound to yours EJB class and transaction management. C # makes metadata have an integral part of this language. XDoclet is an example of another action of metadata. If you have used the xDoclet to generate an EJB file and publish a descriptor, you will know the power of the metadata. In JDK1.5, the metadata is joined in the Java language, JCP agrees. (See JSR175). Although the JSR175 becomes a fact, a good AOP framework should also provide a mechanism to define type metadata that is valid at runtime. Pointcuts If the interceptor, the introduction, the introduction is the feature of AOP, then PointCuts is an adhesive. PointCuts tells the AOP framework, those interceptors bind to those classes, what the original data will be applied to those classes or that introduction will be incoming those classes. PointCuts Defines how various AOP features will be applied to classes in your application. AOP Example 1 in the action. With the interceptor JBoss 4.0 with an AOP framework. This framework is closely combined with the JBoss application server, but you can also run it separately in your application. Until you see it in the action, you will fully understand this concept, so let us use an example from JBoss AOP to how all the modules work together. In this section, we will establish an example to track the framework of the AOP. Define an interceptor in order to implement our tracking for the framework, the first thing we must make is to define an interceptor, which will work with actual work. In JBoss AOP, all interceptors must implement org.jboss.Aop.Interceptor interface. Public Interface Interceptor, PUBLIC INTERCEPTOR

{

Public string getname ();

Public InvocationResponse Invoke (Invocation Invocation)

Throws throwable;

}

In JBoss AOP, all domains, constructors and methods being intercepted are converted to a general invoke call. The parameters of the method are filled in an Invocation object, and the return value of the method, the domain accessor or constructor is filled in an InvocationResponse object. This Invocation object also drives this intercept chain. To clearly illustrate this, let's take a look, in this example, all objects work together.

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

{

// do nothing for fields. Just Too Verbose.

// Don't do anything for the domain. Too cumbersome.

Return invocation.invokenext ();

}

System.out.println (Entering Message);

// Continue on. Invoke The Real Method or Constructor.

// carry on. Calling a true method or constructor

InvocationResponse RSP = Invocation.Invokenext ();

System.out.println (Leaving Message);

Return RSP;

}

}

The above interceptor will intercept all the calls to one domain, constructor, or method. If the type of call is a method or constructor, a message with a method or constructor signature will be output to the control platform. The binding interceptor is ok, so we define the interceptor. But how to bind this interceptor to the actual class? In order to do this, we need to define a PointCut. For JBoss AOP, PointCuts is defined in an XML file. Let us look at this look like what.

The above PointCut binding TracingInterceptor to a class called Pojo. This looks a little trouble; we have to create a PointCut for every class who wants to track? Fortunately, Interceptor-PointCut class properties can be used in any regular expression. So if you want to track the class loaded by JVM, class expressions will become. *. If you just want to track a specific package, the expression will be com.acme.mypackge. *. When running JBoss AOP alone, any XML file that meets the META-INF / JBOSS-AOP.XML mode will be loaded by the JBoss AOP runtime. If the associated path is included in any JAR or your classpath directory, the specific XML file will be loaded by the JBoss AOP runtime when startup. Run this example we will run an example with the PointCut defined above. The POJO class looks 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 ();

}

}

TracingInterceptor will intercept the call to main (), pojo () and helloworld (). The output looks as follows:

ENTERING METHOD: Main

Entering Construction: Public Pojo ()

Leaving constructor: Public Pojo ()

Entering Method: HelloWorld

Hello World!

Leaving method: HelloWorld

Leaving method: main

You can download JBoss AOP and ion code here. Compile and execute:

$ CD OREILLY-AOP / EXAMPLE1

$ export classpath = .; jboss-common.jar; jboss-aop.jar; javassist.jar

$ javac * .java

$ java -djava.system.class.loader = org.jboss.aop.standalone.

SystemClassLoader Pojo

JBoss AOP does a band code operation for the bound interceptor. Because there is no compilation step, the AOP runtime must have the total control of ClassLoader. If you are running in a non-JBOSS application server, you must use JBoss to develop a class loader to overwrite the class loader. TraceingInterceptor does not track domain access as it is cumbersome. For developers, implementing GET () and set () methods to encapsulate domain access is a general practice. If TracingInterceptor is able to filter out and not track these methods, it is very good. This example shows that you can use JBoss AOP metadata to implement filtering based on any method. Typical, metadata is used for more complex things, such as defining transaction properties, security roles or persistence mappings of each method, but this example should sufficiently indicate how metadata can be used in AOP enabled applications. In order to increase this filtering function in order to increase this filtering function, we will use this label to turn off track. We will return to our AOP XML file to define the label, which will delete tracking of the GET () and set () methods. In fact, there is meaningless to track the main () function, so we also filter out it.

true

true

The above XML defines a set of properties called Tracing. This filtration property will be bound to each method starting with GET or SET. Regular expression format The expression defined by JDK-1.4. The metadata is accessed within TracingInterceptor through the Invocation object.

转载请注明原文地址:https://www.9cbs.com/read-112990.html

New Post(0)