0% found this document useful (0 votes)
2 views16 pages

Practical 5

This document outlines the implementation of Aspect-Oriented Programming (AOP) through various programming examples, including classes for car, bike, and airplane, along with an engine aspect. It details AOP concepts such as aspects, join points, advice, and pointcuts, and provides code snippets demonstrating the execution of methods with AOP advice. The document concludes with a successful implementation of AOP concepts.

Uploaded by

pkept8g8sa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Practical 5

This document outlines the implementation of Aspect-Oriented Programming (AOP) through various programming examples, including classes for car, bike, and airplane, along with an engine aspect. It details AOP concepts such as aspects, join points, advice, and pointcuts, and provides code snippets demonstrating the execution of methods with AOP advice. The document concludes with a successful implementation of AOP concepts.

Uploaded by

pkept8g8sa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ATHARVA KALE PRACTICAL 5 ROLL NO: 27

TITLE: Implementation Of AOP

AIM: Assignment based on AOP

 THEORY:
1. Aspect oriented programming (AOP) as the name suggests uses
aspects in programming.
2. Aspect-oriented programming (AOP) is a programming paradigm that
aims to increase modularity by allowing the separation of cross-
cutting concerns.
3. It does so by adding additional behavior to existing code (an advice)
without modifying the code itself, instead separately specifying
which code is modified via a "pointcut" specification
4. Aspect-Oriented Programming entails breaking down program logic
into distinct parts called so-called concerns.
5. The functions that span multiple points of an application are called
cross-cutting concerns
6. AOP Terminologies:
a. Aspect: This is a module which has a set of APIs providing cross-
cutting requirements.
b. Join point: This represents a point in your application where you
can plug-in the AOP aspect.
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
c. Advice: This is the actual action to be taken either before or after
the method execution.
d. Pointcut: This is a set of one or more join points where an advice
should be executed.
e. Introduction: An introduction allows you to add new methods or
attributes to the existing classes.
f. Target object: The object being advised by one or more aspects.
g. Weaving: Weaving is the process of linking aspects with other
application types or objects to create an advised object.

7. Types Of Advice:
a. Before Advice: Run advice before the method execution.
b. After Advice: Run advice after the method execution,
regardless of its outcome.
c. Around Advice: Run advice before and after the advised
method is invoked.
d. Throws Advice: Run advice if actual method throws exception
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
Q1) Create class car, bike, airplane, create an aspect engine, create
method drive() in car, ride() in bike, fly in airplane, the engine aspect
has method enginestart() and enginestop(). When you run the
application the enginestart() method should execute before drive(),
ride() and fly() a enginestop() method should run after drive(), ride()
and fly().

CODE:

a. car.java

package some.pro1;
import org.springframework.stereotype.Component;
@Component
public class car {
public void drive()
{
System.out.println("CAR STARTED DRIVING");
}

b. bike.java

package some.pro1;
import org.springframework.stereotype.Component;
@Component
public class bike {
public void ride()
{
System.out.println("Biker riding bike");
}
}

c. airplane.java
ATHARVA KALE PRACTICAL 5 ROLL NO: 27

package some.pro1;
import org.springframework.stereotype.Component;
@Component
public class airplane {
public void fly()
{
System.out.println("Airplane flying");
}
}

d. engine.java

package some.pro1;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
import
org.springframework.context.annotation.EnableAspectJAutoProx
y;
import org.springframework.stereotype.Component;

@Component
@Aspect
@EnableAspectJAutoProxy

public class engine {

@Before("execution(public void drive())")


public void enginestart()
{
System.out.println("Enigine started");
}

@After("execution(public void fly())")


public void enginestop()
{
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
System.out.println("Enigine stopped");
}

e. app.java
package some.pro1;
import
org.springframework.context.annotation.AnnotationConfigApplicationCo
ntext;
import org.springframework.context.support.AbstractApplicationContext;

public class App


{
public static void main( String[] args )
{
AbstractApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
car c = context.getBean("car",car.class);
c.drive();
bike b = context.getBean("bike",bike.class);
b.ride();
airplane a = context.getBean("airplane",airplane.class);
a.fly();
}
}
ATHARVA KALE PRACTICAL 5 ROLL NO: 27

OUTPUT:

Q2) Create class car, bike, airplane, create an aspect engine, create
method drive() in car, ride() in bike, fly in airplane, the engine aspect
has method enginestart() and enginestop(). When you run the
application the enginestart() method should execute before drive(),
ride() and fly() a enginestop() method should run after drive(), ride()
and fly() by using pointcuts.

CODE:

a. car.java

package something.pro2;

import org.springframework.stereotype.Component;

@Component
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
public class car {

public void drive()


{
System.out.println("CAR STARTED DRIVING");
}
}

b. bike.java

package something.pro2;

import org.springframework.stereotype.Component;

@Component

public class bike {

public void ride()


{
System.out.println("Biker riding bike");
}
}

c. airplane.java

package something.pro2;

import org.springframework.stereotype.Component;

@Component

public class airplane {

public void fly()


ATHARVA KALE PRACTICAL 5 ROLL NO: 27
{
System.out.println("Airplane Flying");
}
}

d. engine.java

package something.pro2;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Aspect;
import
org.springframework.context.annotation.EnableAspectJAutoProx
y;
import org.springframework.stereotype.Component;

@Component
@Aspect
@EnableAspectJAutoProxy

public class engine {

@Before("getNamePointcut()")
public void enginestart()
{
System.out.println("Engine started");
}
@Pointcut("execution(public void drive())")

public void getNamePointcut(){}

@After("getNamePointcut1()")

public void enginestop()


{
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
System.out.println("Engine stopped");
}

@Pointcut("execution(public void fly())")


public void getNamePointcut1(){}

e. app.java

package something.pro2;

import
org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

public class App

public static void main( String[] args )

AbstractApplicationContext context = new

AnnotationConfigApplicationContext(AppConfig.class);

car c = context.getBean("car",car.class);

c.drive();

bike b = context.getBean("bike",bike.class);

b.ride();
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
airplane a = context.getBean("airplane",airplane.class);

a.fly();

OUTPUT:

Q3) Create a business class multiplier (int a, int b) which returns


product of a and b create an aspect AdderAfterReturnAspect use
After-returning advice.

CODE:

a. multiplier.java

package something.pro3;

import org.springframework.stereotype.Component;
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
@Component

public class multiplier {

public int mul(int a,int b)


{
return a*b;
}

b. AdderAfterReturnAspect.java

package something.pro3;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
import
org.springframework.context.annotation.EnableAspectJAutoProx
y;
import org.springframework.stereotype.Component;

@Component
@Aspect
@EnableAspectJAutoProxy

public class AdderAfterReturnAspect {


@AfterReturning(pointcut="execution(*
something.pro3.multiplier.*(..))", returning="retVal")
public void afterreturning(Object retVal)
throws Throwable
{
System.out.println("Multiplication
is:"+retVal+"");
}
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
}

c. App.java

package something.pro3;

import
org.springframework.context.annotation.AnnotationConfigAppli
cationContext;
import
org.springframework.context.support.AbstractApplicationConte
xt;

public class App


{
public static void main( String[] args )
{
AbstractApplicationContext context = new

AnnotationConfigApplicationContext(AppConfig.class);

multiplier
m=context.getBean("multiplier",multiplier.class);
m.mul(5, 5);
}
}

OUTPUT:
ATHARVA KALE PRACTICAL 5 ROLL NO: 27

Q4) Create a voter class with attribute name and age create an
exception check if age is less than 18 throw exception. Create and
aspect illegalVoter with a method if the voter class throws exception
the illegalVoter aspect method will run

CODE:

a. voter.java

package something.pro4;

import org.springframework.stereotype.Component;

@Component

public class voter {


public void validate() throws Exception
{
int age=19;
String name="Tony";

if(age<18)
{
throw new Exception(name+" you are under age!");
}
else
{
System.out.println("Do vote "+name+" as your age is
"+age);
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
}

}
}

b. illegalvoter.java

package something.pro4;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import
org.springframework.context.annotation.EnableAspectJAutoProx
y;
import org.springframework.stereotype.Component;

@Component
@Aspect
@EnableAspectJAutoProxy

public class illegalvoter {


@AfterThrowing(pointcut="execution(*
something.pro4.voter.*(..))",throwing = "ex")
public void illegalVoter(Exception ex) throws
Throwable
{

System.out.println("LoggingAspect.logAfterThrowingAllMethods
() "+ ex);
}
}
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
c. app.java

package something.pro4;

import
org.springframework.context.annotation.AnnotationConfigAppli
cationContext;

import
org.springframework.context.support.AbstractApplicationConte
xt;

public class App


{
public static void main( String[] args ) throws
Exception
{
AbstractApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
voter v=context.getBean("voter",voter.class);
v.validate();
}
}

OUTPUT:
ATHARVA KALE PRACTICAL 5 ROLL NO: 27

 CONCLUSION:
Hence concept of AOP implemented successfully.

You might also like