Aspective programming (AOP) and JBoss

xiaoxiao2021-03-06  45

NEO translation (reprinted from Matrix Open Source Technology) October 02, 2004

Overview

Aspect-Oriented 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 is triggered by 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.

preface

The introduction is a way to increase methods or domains into a class that exists. 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); Introduction can be a method that binds a new API to an object model that exists.

Metadata

Metadata is an additional information that is capable of binding to a class, 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 introducer, and metadata 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 in action

example 1. Use interceptors

JBoss 4.0 has 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 achieve our tracking of the framework, the first thing we must make is to define an interceptor, which will work actually. In JBoss AOP, all interceptors must implement org.jboss.Aop.Interceptor interface.

Public interface interceptor {public string getname (); publication infocation 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.to} else {// do nothing for fields. Just Too Verbose.// The domain does not do. Too cumbersome. Return invocation.invokenext ();

System.out.println (Entering Message);

// Continue on. Invoke the real method or constructor.// Continue. Call the true method or constructor invocation.invokeNext (); system.out.println; 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. Binding interceptor

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.

Access Metadata

In order to use metadata, it must be up to date. The metadata of the class is accessible through the Invocation object. In order to use it in our example, TracingInterceptor must modify a little bit.

public class TracingInterceptor implements Interceptor {public String getName () {return TracingInterceptor;} public InvocationResponse invoke (Invocation invocation) throws Throwable {String filter = (String) invocation.getMetaData (tracing, filter);! if (filter = null && filter. Equals (TRUE)) Return Invocation.Invokenext ();

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.return invocation.invokeNext ();} System.out.println ( ENTERING Message);

// Continue on. Invoke the real method or constructor.invocationResponse RSP = INVOCATION.INVOKENEXT (); system.out.println (leaving mess); RETURN RSP;}} Run Example 2: Pojo class will extends a little, increase Get ( ) And set () methods.

Public Class Pojo {public pojo () {} public void helloworld () {system.out.println (Hello World!);

Private int counter = 0;

Public void setcounter (} public void setcounter (int val) {counter = val;} public static void main (string [] args) {pojo pojo = new pojo (); pojo.helloWorld (); pojo. SetCounter (32); System.out.println (Counter is: pojo.getcounter ());}} TracingInterceptor will intercept main (), pojo (), and helloworld () calls. The output should appear as follows:

Entering Construction: Public Pojo () Leaving Construction: Public Pojo () Entering Method: HelloWorldhello World! Leaving Method: HelloWorld You can download JBoss AOP and ion code here. Compile and execute: $ cd oreilly-aop / example2

$ export classpath = .; jboss-common.jar; jboss-aop.jar; javassist.jar $ javac * .java $ java -djava.system.class.loader = org.jboss.aop.standalone.systemclassloader Pojo Example 3. Use language

If we can turn off and open for a specific instance, it will be cool. The JBoss AOP has an API that binds the metadata to an object instance, but let us disguise an actual tracking API is a better solution. In this case, we will change the definition of the Pojo class by using a note. We will force the POJO class to implement a tracking excuse and provide a mixed class, which processes the new tracking API. This will be tracking excuses:

Public interface traction {public void enableTracing (); public void disableTracing ();} Defines a mixed class Tracing interface will be implemented in the hybrid class. When a POJO is an example, a mixed object mix class will bind to the Pojo class. The following is true: import org.jboss.aop.advised;

Public Class TracingMixin Implements Tracing {Advised Advised;

Public TracingMixin (Object Obj) {this.advised = (advised) Obj;}

Public void enableTracing () {advised._getinstanceadvisor (). getmetadata (). Addmetadata ("Tracing", "Filter", True;

Public void disableTraacing () {advised._getinstanceAdvisor (). getMetadata (). AddMetadata ("Tracing", "Filter", false;}} enableTraacing () method Binds the Filter property to the object instance. The DisableTraacing () method is the same, but the Filter property is developed to false. These two methods are how metadata can be used for more than one class. Metadata can also instance level applications. Metadata application in instance level.

Bind a bet

Ok, so we define the trace interface and implement this mixed class. The next step is to apply the introduction to the POJO class. Like interceptors, we must define a Ponitcut in XML. Let us look at what this item.

Tracing TracingMixin New TracingMixin (this) The POINTCUTS above will force the POJO class to implement the TRACING interface. Now, when a Pojo instance is initialized, a TracingMixin will also be instantiated. TracingMixin is initialized to be defined in the label. You can put the Java code you want to place in the label.

Operation example 3

The POJO class has been expanded in order to display TracingAPI. TracingInterceptor is still the same as the example 2.

Public Class Pojo {public pojo () {} public void helloworld () {system.out.println (Hello World!);

Public static void main (string [] args) {pojo pojo = new pojo (); traction trace = (traction) this; pojo.helloworld ();

System.out.println ("Turn Off Tracing.");

TRACE.DISABLETRACING (); pojo.helloworld ();

System.out.Println ("Turn on Tracing.");

TRACE.enableTraacing (); pojo.helloworld ();}} Note We convert the POJO to the TRACING interface. The output should look like this: Entering constructor: POJO () Leaving constructor: POJO () Entering method: helloWorldHello World Leaving method:! HelloWorldTurn off tracing.Entering method: disableTracingLeaving method:! DisableTracingHello World Turn on tracing.Entering method: helloWorldHello World! Leaving Method: HelloWorld Note Interceptor-Pointcut in TracingInterceptor is also applied to those methods imported through TRACING introduction. To compile and run this example: $ cd oreilly-aop / example3

$ export classpath = .; jboss-common.jar; jboss-aop.jar; javassist.jar $ javac * .java $ java -djava.system.class.loader = org.jboss.aop.Standalone.systemclassloader Pojo Conclusion

Aspects of programming is a powerful new tool for software development. In order to make your software development process more dynamic and smooth, you can implement your own interceptors, metadata and inscriptions. See the site www.jboss.org more detailed documentation.

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

New Post(0)