0% found this document useful (0 votes)
28 views

Module 4 Assignments

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Module 4 Assignments

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Assignment No 8 : Assignment based Aspect Oriented

Programming

1. Write a program to demonstrate Spring AOP – before advice.

2. Write a program to demonstrate Spring AOP – after advice.

3. Write a program to demonstrate Spring AOP – around advice.

4. Write a program to demonstrate Spring AOP – after returning advice.

5. Write a program to demonstrate Spring AOP – after throwing advice.

6. Write a program to demonstrate Spring AOP – pointcuts


AOP TERMINOLOGY
What is advice, joinpoint or pointcut?
Aspect : This is a module which has a set of APIs providing cross-cutting requirements.
● For example, a logging module would be called AOP aspect for logging. An application can
have any number of aspects depending on the requirement.
It can be normal class configured through XML configuration or through regular
classes annotated with @Aspect.

Join point : This represents a point in your application where you can plug-in the AOP
aspect. You can also say, it is the actual place in the application where an action will be taken
using Spring AOP framework.
● An application has thousands of opportunities or points to apply Advice. These points are
known as join points. For example – Advice can be applied at every invocation of a
method or exception be thrown or at various other points. But Spring AOP currently
supports only method execution join points (advising the execution of methods on Spring
beans).
Join point is any point in your program such as method execution, exception handling,
field access etc. Spring supports only method execution join points
Advice :
Advice represents an action taken by an aspect at a particular join point.
There are different types of advices:

● Before Advice: it executes before a join point.

● After Returning Advice: it executes after a joint point completes normally.

● After Throwing Advice: it executes if the method exits by throwing an


exception.

● After (finally) Advice: it executes after a join point regardless of join point exit
whether normally or exceptional return.

● Around Advice: It executes before and after a join point


Pointcut :
This is a set of one or more join points where an advice should be executed. You
can specify pointcuts using expressions or patterns.
Introduction : An introduction allows you to add new methods or attributes to
the existing classes.
Target object (java Class): The object being advised by one or more aspects
(class). This object will always be a proxied object, also referred to as the advised
object.
Weaving : Weaving is the process of linking aspects with other application types
or objects to create an advised object. This can be done at compile time, load
time, or at runtime.Spring AOP performs weaving at runtime.
AOP Proxy : AOP Proxy use for implementing the aspect contracts which create by
AOP Framework. It will be a JDK dynamic proxy in Spring Framework.
Interceptor : It is an aspect which contains advice, joinpoints etc.
Aspect Oriented Programing

Spring AOP can be used in 3 ways given below. But the widely used approach is
Spring AspectJ Annotation Style.

The 3 ways to use spring AOP are given below:

1. By Spring1.2 Old style (dtd based) (also supported in Spring3)

2. By AspectJ annotation-style

3. By Spring XML configuration-style(schema based)


1. Write a program to demonstrate Spring AOP – before advice.

2. Write a program to demonstrate Spring AOP – after advice.


NOTE:
FOR

Eclipse IDE Version: 2021-09 (4.21.0) USE ABOVE MENTIONED JAR

MODULE SPRING AOPJars-20220309T075019Z-001.zip

Eclipse IDE Version: 2021-09 (4.21.0)

FOR Eclipse IDE 2022‑12 USE Spring_AOP_Jar_Files(2).zip

Jar Drive Link :


https://drive.google.com/drive/folders/1DfTCzrqkP2MSMQhCuCKgJrwUSOMw5Cby?usp=sharing
POJO CLASS
Main File to load config file
Aspect : Specialized class
package com.aop;

public class LoggingAspect {


//user defined methods ==> cross-cutting concern / Non-Functional

public void beforeadvice() {


System.out.println("I am BEFORE ADVICE");
}

public void afteradvice() {


System.out.println("I am AFTER ADVICE");
}
}
Configuration using AOP
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop = "http://www.springframework.org/schema/aop"

xsi:schemaLocation = "http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd "

>
<bean id="stud" class="com.aop.Student">

<property name="ID" value="56"></property>

<property name="NAME" value="Shweta Waghmare"></property>

</bean>

<bean id="logbean" class="com.aop.LoggingAspect"></bean>


AOP Configuration

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect id="log" ref="logbean">
<aop:before method="beforeadvice" pointcut="execution(public void
display())"/>
<aop:after method="afteradvice" pointcut="execution(public void
display())"/>
</aop:aspect>
</aop:config>
</beans>
Around Advice
Around is an advice type which ensures that an advice runs before and after the method execution.

Syntax
<aop:config>

<aop:aspect id = "log" ref = "logging">

<aop:pointcut id = "PointCut-id" expression = "execution( expression )"/>

<aop:around pointcut-ref = "PointCut-id" method = "methodName"/>

</aop:aspect>

</aop:config>

Where,

● PointCut-id − id of the PointCut.


● methodName − Method name of the function to be called before a called function.
3. Write a program to demonstrate Spring AOP – around advice.

package com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggingAspect {
public void aroundadvice(ProceedingJoinPoint jp) throws Throwable {
System.out.println("Student data(Around Advice)");
jp.proceed();
System.out.println("Above data is of required student(Around
Advice)");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
>
<bean id="stud" class="com.aop.Student">
<property name="ID" value="56"></property>
<property name="NAME" value="Shweta Waghmare"></property>
</bean>
<bean id="l" class="com.aop.LoggingAspect"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect id="LoggingAspect" ref="l">
<aop:around method="aroundadvice" pointcut="execution(public void display())"/>
</aop:aspect>
</aop:config>
</beans>
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentMainApp {
public static void main(String[] args) {
ApplicationContext context =new
ClassPathXmlApplicationContext("studentconfig.xml");
Student s= (Student) context.getBean("stud");
s.display();
}
}

//Main File
4. Write a program to demonstrate Spring AOP – after returning advice.

package com.aop;
public class LoggingAspect {
public void afterreturnadvice(Object retval) {
System.out.println("Method got successfully executed (After Returning
(value) Advice )");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd config.xml
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
>
<bean id="stud" class="com.aop.Student">
<property name="ID" value="56"></property>
<property name="NAME" value="Shweta Waghmare"></property>
</bean>
<bean id="l" class="com.aop.LoggingAspect"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy><aop:config>
<aop:aspect id="LoggingAspect" ref="l">
<aop:after-returning method="afterreturnadvice" returning="retval"
pointcut="execution(public void display())"/>
</aop:aspect></aop:config>
</beans>
Main method

package com.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentMainApp {

public static void main(String[] args) {

ApplicationContext context =new


ClassPathXmlApplicationContext("studentconfig.xml");
Student s= (Student) context.getBean("stud");
s.display();
}

}
5. Write a program to demonstrate Spring AOP – after throwing advice.

package com.aop;
public class Student {
private int ID;
private String NAME;
private int age; Student.java
//getter and setter
public void display() {
System.out.println("ID:" + ID);
System.out.println("Name:" + NAME);
}
public void validate(int age) {
if (age < 18) {
throw new ArithmeticException();
} else {
System.out.println("Valid age");
}
}
}
LoggingAspect .java

package com.aop;

public class LoggingAspect {

public void afterexceptionadvice(Exception error) {


System.out.println("Some Exception Occured");
System.out.println(error);
}
}
config.xml

<aop:aspect id="LoggingAspect" ref="l">

<aop:after-throwing method="afterexceptionadvice" throwing="error"

pointcut="execution(public void validate(*))"/>

</aop:aspect></aop:config>
Main App
public class StudentMainApp {
public static void main(String[] args) {
ApplicationContext context =new
ClassPathXmlApplicationContext("studentconfig.xml");
Student s= (Student) context.getBean("stud");
s.display();
System.out.println("After throwing advice:");
s.validate(24);
}
}

You might also like