Aspect oriented programming(AOP)
Aspect oriented programming(AOP)
Aspect oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Aspects enable the implementation of crosscutting concerns such as- transaction, logging not central to business logic without cluttering the code core to its functionality. It does so by adding additional behaviour that is the advice to the existing code. For example- Security is a crosscutting concern, in many methods in an application security rules can be applied, therefore repeating the code at every method, define the functionality in a common class and control were to apply that functionality in the whole application.
Dominant Frameworks in AOP:
AOP includes programming methods and frameworks on which modularisation of code is supported and implemented. Let’s have a look at the three dominant frameworks in AOP:
- AspectJ: It is an extension for Java programming created at PARC research centre. It uses Java like syntax and included IDE integrations for displaying crosscutting structure. It has its own compiler and weaver, on using it enables the use of full AspectJ language.
- JBoss: It is an open source Java application server developed by JBoss, used for Java development.
- Spring: It uses XML based configuration for implementing AOP, also it uses annotations which are interpreted by using a library supplied by AspectJ for parsing and matching.
Currently, AspectJ libraries with Spring framework are dominant in the market, therefore let’s have an understanding of how Aspect-oriented programming works with Spring.
How Aspect-Oriented Programming works with Spring:
One may think that invoking a method will automatically implement cross-cutting concerns but that is not the case. Just invocation of the method does not invoke the advice(the job which is meant to be done). Spring uses proxy based mechanism i.e. it creates a proxy Object which will wrap around the original object and will take up the advice which is relevant to the method call. Proxy objects can be created either manually through proxy factory bean or through auto proxy configuration in the XML file and get destroyed when the execution completes. Proxy objects are used to enrich the Original behaviour of the real object.
Common terminologies in AOP:
- Aspect: The class which implements the JEE application cross-cutting concerns(transaction, logger etc) is known as the aspect. It can be normal class configured through XML configuration or through regular classes annotated with @Aspect.
- Weaving: The process of linking Aspects with an Advised Object. It can be done at load time, compile time or at runtime time. Spring AOP does weaving at runtime.
Let’s write our first aspect class but before that have a look at the jars required and the Bean configuration file for AOP.
Comments
Post a Comment