Test Driven Development
Aspect Oriented Programming
Annotation Based Programming
Inversion Of Control - Part 1
Inversion Of Control - Part 2
Hibernate Interview Questions - Part 1
Hibernate Interview Questions - Part 2
Hibernate Interview Questions - Part 3
Spring Interview Questions
EJB 3.0 Interview Questions
Aspect Oriented Programming Interview Questions
What is aspect oriented programming (AOP) ? Do you have any experience with AOP ?Aspect-Oriented Programming (AOP) complements OOP (Object Oriented Programming) by allowing the developer to dynamically modify the static OO model to create a system that can grow to meet new requirements. AOP allows you to dynamically modify your static model consisting mainly of business logic to include the code required to fulfill the secondary requirements or in AOP terminology called cross-cutting concerns (i.e. secondary requirements) like auditing, logging, security, exception handling etc without having to modify the original static model (in fact, we don't even need to have the original code). Better still, we can often keep this additional code in a single location rather than having to scatter it across the existing model, as we would have to if we were using OOP on its own.
For example:A typical Web application will require a servlet to bind the HTTP request to an object and then pass it to the business handler object to be processed and finally return the response back to the user. So only a minimum amount of code is initially required. But once you start adding all the other additional secondary requirements or cross-cutting concerns like logging, auditing, security, exception-handling, transaction demarcation, etc the code will inflate to 2-4 times its original size. This is where AOP can assist by separately modularizing these cross-cutting concerns and integrating theses concerns at runtime or compile time through aspect weaving. AOP allows rapid development of an evolutionary prototype using OOP by focusing only on the business logic by omitting concerns such as security, auditing, logging etc. Once the prototype is accepted, additional concerns like security, logging, auditing etc can be weaved into the prototype code to transfer it into a production standard application.
AOP nomenclature is different from OOP and can be described as shown below:
Join points:represents the point at which a cross-cutting concern like logging, auditing etc intersects with a main concern like the core business logic. Join points are locations in programsí execution path like method & constructor call, method & constructor execution, field access, class & object initialization, exception handling execution etc.
Pointcut:is a language construct that identifies specific join points within the program. A pointcut defines a collection of join points and also provides a context for the join point.
Advice:is an implementation of a cross-cutting concern which is a piece of code that is executed upon reaching a pointcut within a program.
Aspect:encapsulates join points, pointcuts and advice into a reusable module for the cross-cutting concerns which is equivalent to Java classes for the core concerns in OOP. Classes and aspects are independent of one another. Classes are unaware of the presence of aspects, which is an important AOP concept. Only pointcut declaration binds classes and aspects.
Weavingis the process for interleaving separate cross-cutting concerns such as logging into core concerns such as business logic code to complete the system. AOP weaving composes different implementations of aspects into a cohesive system based on weaving rules. The weaving process (aka injection of aspects into Java classes) can happen at:
So which approach to use ?Load-time and runtime weaving have the advantages of being highly dynamic and enabling changes on the fly without having to rebuild and redeploy. But Load-time and runtime weaving adversely affect system performance. Compile time weaving offers better performance but requires rebuilding and redeployment to effect changes.
Do you have any experience with AOP ?Two of the most interesting modules of the Spring framework are AOP (Aspect Oriented Programming) and Inversion Of Control (IoC) container (aka Dependency Injection). Let us look at a simple AOP example.
STEP 1:Define the interface and the implementation classes. Spring promotes the code to interface design concept.
STEP 2:Configure the Spring runtime via the SpringConfig.xml file. Beans can be configured and subsequently injected into the calling Test class.
STEP 3:Write your Test class. The "SpringConfig.xml" configuration file should be in the classpath.
If you run the Test class, you should get an output of :
Now, if you want to trace your methods like hello() before and after in your Hello class, then you can make use of the Spring AOP.
STEP 4:Firstly you need to define the classes for the before and after advice for the method tracing as follows:
STEP 5:In order to attach the advice to the appropriate joint points, you must make a few amendments to the SpringConfig.xml file as shown below in bold:
If you run the Test class again, you should get an output with AOP in action:
Spring offers declarative transaction management. This is enabled by Spring AOP. Declarative transaction management should be preferred over programmatic transaction management since it is non-invasive and has least impact on your application code. Not only transaction management but also other system level services like security, logging, auditing etc should be implemented declaratively with the AOP feature.
What are the differences between OOP and AOP ?
Object Oriented Programming (OOP)
Aspect Oriented Programming (AOP)
What are the benefits of AOP ?
Annotation Based Programming Interview Questions>>>