Practical 5
Practical 5
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
e. app.java
package some.pro1;
import
org.springframework.context.annotation.AnnotationConfigApplicationCo
ntext;
import org.springframework.context.support.AbstractApplicationContext;
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 {
b. bike.java
package something.pro2;
import org.springframework.stereotype.Component;
@Component
c. airplane.java
package something.pro2;
import org.springframework.stereotype.Component;
@Component
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
@Before("getNamePointcut()")
public void enginestart()
{
System.out.println("Engine started");
}
@Pointcut("execution(public void drive())")
@After("getNamePointcut1()")
e. app.java
package something.pro2;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
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:
CODE:
a. multiplier.java
package something.pro3;
import org.springframework.stereotype.Component;
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
@Component
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
c. App.java
package something.pro3;
import
org.springframework.context.annotation.AnnotationConfigAppli
cationContext;
import
org.springframework.context.support.AbstractApplicationConte
xt;
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
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
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;
OUTPUT:
ATHARVA KALE PRACTICAL 5 ROLL NO: 27
CONCLUSION:
Hence concept of AOP implemented successfully.