You are on page 1of 8

Experiment No.

: 2

Aim: Create a Java application that uses AOP. Run the application while modifying it and every time record the
output.

Theory:
1) Aspect Oriented Programming: Aspect Oriented Programming (AOP) compliments OOPs in the sense
that it also provides modularity. But the key unit of modularity is aspect than class.
AOP breaks the program logic into distinct parts (called concerns). It is used to increase modularity
by cross-cutting concerns.
A cross-cutting concern is a concern that can affect the whole application and should be centralized in
one location in code as possible, such as transaction management, authentication, logging, security etc.

AOP concepts and terminologies are as follows:


o Join point
o Advice
o Pointcut
o Introduction
o Target Object
o Aspect
o \Weaving

2) Aspect: Corresponds to the class in object-oriented programming. It's the crosscutting functionality. A
structure analogous to a Java class that encapsulates join points, pointcuts, advice, and inter-type
declarations.

3) Pointcut: This is the expression for the JoinPoints selection, for instance a method's execution with a
certain signature (WHERE).

Procedure: Steps to create Aspect project.

1. To create the AspectJ Program, Go on File >>New >>Other >>Click on AspectJ Project.
2. Click on Next button. Create an AspectJ Project dialog box will appear. define the name for your project
i.e. AOP_Tutorial and click on Finish Button.

3. New Module-info.java dialog box will appear. Create Module name i.e. AOP_Tutorial. After that click
on Create button.
4. The AOP_Tutorial module has been created. In AOP_Tutorial src file has been added with other two
files namely, JRE System Library and AspectJ Runtime Library.

5. To create package, go on AOP_Tutorial >>src >> Right click >>New >>Package. Create Java Package
dialog box will appear, define the name of the package i.e. com.aop.gbu, then click on Finish Button.
6. To create java class, go on AOP_Tutorial >>src >> Right click >>New >>Class. Create Java class dialog
box will appear, define the name of the class i.e. AOPDemo. Mark a tick on checkbox named public
static void main (String[] args). Then click on finish Button.

7. To create aspect, go to AOP_Tutorial>> src>> com.aop.gbu>> Right Click>> New>> Others. Dialog box
will appear, select the wizard as Aspect. Click on Next button. Then new dialog box of create aspect
will appear, define its name i.e. Demoaspect. Then click on Finish Button.
8. Now both the java class as well as aspect has been created under package com.aop.gbu, namely
AOPDemo.java as class and Demoaspect.aj as aspect.

9. Now Start coding on your respective Software i.e. Eclipse.

10. Now, to have output click on Run >> AspectJ /Java Development. And then click Ok Button. The
output will appear on output console.
Coding:
AOPDemo.java file

package com.aop.gbu;

public class AOPDemo {

public static void main(String[] args) {


AOPDemo aOPDemo=new AOPDemo();
aOPDemo.method1(25);
aOPDemo.method1(23, "stringvalue");
aOPDemo.method2("stringvalue");

}
public void method1(int number) {
System.out.println("executing method1(int)code here...");
}
public void method1(int number,String value) {
System.out.println("executing method1(int,string)code here...");
}
public void method2(String value) {
System.out.println("executing method2(string)code here...");
}

Demoaspect.aj file:

a) Method 1(*)

package com.aop.gbu;

public aspect Demoaspect {


pointcut callDemoaspectPointcut():
call (void com.aop.gbu.AOPDemo.method1*(*));
after() :callDemoaspectPointcut(){
System.out.println("Demo Aspect Point Cut after....");
}
}

Output:
b) Method 2 (*, *)
package com.aop.gbu;

public aspect Demoaspect {


pointcut callDemoaspectPointcut():
call (void com.aop.gbu.AOPDemo.method1(*,*));
after() :callDemoaspectPointcut(){
System.out.println("Demo Aspect Point Cut after....");
}
}

Output:

c) Method 3*(..)
package com.aop.gbu;

public aspect Demoaspect {


pointcut callDemoaspectPointcut():
call (void com.aop.gbu.AOPDemo.method1*(..));
after() :callDemoaspectPointcut(){
System.out.println("Demo Aspect Point Cut after....");
}
}

Output:

You might also like