Overview
Aspect-oriented programming (AOP) is an exciting new specification that has the same effect on software development as the more than 10-year-old object-oriented programming (OOP). AOP and OOP are not competing technologies, and in fact they complement each other very harmoniously. Object-oriented programming is useful for modeling common object hierarchies. The disadvantage of this is that it deals with common situations across multiple unrelated object models, where AOP is useful. AOP allows you to use separate, very different object models across associations. It allows you to hierarchically-rather than embed-functions so that the code is easier to read and easier to maintain. We like to think of object-oriented programming as Top-down software development, while aspect-oriented programming is left to right; they are completely orthogonal technologies that complement each other very harmoniously.
Object-oriented program design means inheritance, encapsulation and polymorphism, while aspect-oriented programming components are notifications/listeners (Advice/interceptor), Introduction (Introduction), Metadata (metadata), and Pointcuts (pointcut). Let's take a look at these definitions.
Notification/Listener (Advice/interceptor)
A notification is a program logic that is triggered by an event. It is the behavior that can be inserted between a method caller and the actual method. Notification is actually the key to aspect-oriented program design. These constructs allow you to define crosscutting (cross-cutting) behavior. Notifications allow you to transparently apply things like records and metrics to an existing object model.
In JBoss AOP, we use listeners to implement notifications. You can define listeners to listen for method calls, constructor calls, and field accesses. Later, we'll look at how to apply these listeners to an existing object model.
Introduced
Introduction is a way to add a method or field to an existing class. They even allow you to change the interfaces currently implemented by an existing class and introduce a hybrid class to implement these new interfaces.
Introduction allows you to take multiple inheritance to a simple Java class. An important example of an application is that you have an aspect that you want to have a run-time interface. You want to apply your aspect across different object hierarchies, but you still want the application developer to be able to invoke the specified aspect API.
Apple apple = new Apple();
LoggingAPI logging = (LoggingAPI)apple;
Apple.setLoggingLevel(VERBOSE);
Introduction can be a method of attaching a new API to an existing object model.
Meta data
Metadata is additional information that can be attached to a class, either statically or at run time. It is more efficient when you can dynamically attach metadata to an instance given by an object. Metadata is particularly important when you are writing a generic aspect that can be used for any object, but the program logic must know the information for the specified class. A very similar case of metadata being used is the EJB specification. In the EJB XML configuration descriptor, you define transaction attributes on a per-method basis. The application server knows when and where to start, pause, or delegate a transaction because you have defined required, requiresnew, supports, and so on. In the metadata that your EJB class and transaction manager binds, it is the bean's XML configuration file.
C # has built metadata into the language. Xdoclet is another example of a good meta data that is working. If you've ever used Xdoclet to generate EJB files and configuration descriptors, you certainly know the powerful features of metadata. The Java Community Process (JCP) agreed that metadata was added to JDK 1.5 (see JSR175). Until JSR 175 really becomes a specification, a good AOP framework can provide a mechanism for declaring class-level metadata that is valid at run time.
Pointcut
If the listener, introduction, and metadata are features of aspect-oriented programming design, then the pointcut is the link that links these features. The pointcut tells the aspect-oriented programming framework, which listener will bundle with which class, which metadata will be used for which classes, or what classes will be imported into. Pointcuts define the various aspect-oriented programming features that can be used for your application's classes.